Re: removing perldata , hashref from XML file (updated)
by haukex (Archbishop) on May 13, 2020 at 09:02 UTC
|
I assume this is a followup to your previous post "Run a perl code on all input files and have the results in different output files", so what you've shown here is the Perl data structure returned by TAP3::Tap3edit's $tap3->structure run through XML::Dumper. That module allows me to parse back the XML into the data structure I show below (anonymized, in case this happens to be real data? Update: SaraMirabi confirmed in the CB that it is fake data) - it would have been better if you'd showed us the Perl data structure to begin with, using Data::Dumper or Data::Dump.
I think it would be best if you didn't rely on munging the output of XML::Dumper, but instead build the XML output from the Perl data structure yourself, because that gives you full control over the produced XML. Personally, although it's a bit more verbose, I prefer to write XML with XML::LibXML and some helper functions. The following is an example of how one might go about that, the idea being that you can adapt this however you need.
| [reply] [d/l] [select] |
|
my $data = { employee => [ {
"************" => "M", age => { dob => "10-02-1917" },
department => { departmentname => "Operations",
title => "Manager" },
location => { town => { county => "East Ay1",
name => "Auchinleck" } },
name => { forename => "John", surname => "Doe" },
My xml file is more than 2000 lines with different tags and also it is encoded, I decode it and make it as XML file, introducing each tag in my perl code is not possible.
| [reply] [d/l] |
|
You don't have to introduce the structure. Just use the return value of $tap3->structure.
map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
| [reply] [d/l] [select] |
|
|
|
|
|
|
My exact problem is about introducing each tag in perl code ... introducing each tag in my perl code is not possible.
Sorry, I don't understand what you mean here. Please see Short, Self-Contained, Correct Example and I know what I mean. Why don't you?. As I said, you can adapt the code I've posted (which I just updated) however you need, but you also haven't shown your expected output.
Update: Ah, I see choroba may have interpreted your issue better than I have. Yes, my $data is meant to be the same thing as the $perl variable from your previous node.
| [reply] [d/l] [select] |
|
|
perl DECODEi.pl
Can't handle Math::BigInt=HASH(0x22bcc10) (yet) at DECODEi.pl line 904
+07.
| [reply] [d/l] |
|
Can't handle Math::BigInt=HASH(0x22bcc10) (yet) at DECODEi.pl line 90407.
This error message is surprising in that Math::BigInt overloads stringification, and the error message should not be showing the object name like that. You'd have to show a SSCCE that reproduces this issue to investigate further.*
Given this, I don't know if the following will work in your case, but my code can be extended to support objects that overload stringification by adding the following to the top of the file, just under the other use statements:
use Scalar::Util qw/blessed/;
use overload ();
And this just before the line "elsif ( ref $data ) { die "Can't handle $data (yet)" }":
elsif ( blessed($data) && overload::Overloaded($data)
&& overload::Method($data,'""') )
{ newel($parent, @args)->appendText("$data") }
* Update: It can be explained if you're reading a dump of a Perl data structure into a script that doesn't load the corresponding module. In this case, my code above won't work either unless you load the module. | [reply] [d/l] [select] |
Re: removing perldata , hashref from XML file
by jcb (Parson) on May 13, 2020 at 23:25 UTC
|
Perl might not be the best tool for translating XML as you have asked. Have you looked into using XSLT for this?
A better option could be to change the code that produces the XML. XML::Dumper seems intended to serialize Perl data structures analogously to Data::Dumper, which is probably not what you want. There is probably a module that does exactly what you want, but XML can also be generated directly.
| [reply] |
|
Yes, I checked XSLT and I tried to modify my code but unfortunately, it hasn't worked properly.
| [reply] |
Re: removing perldata , hashref from XML file
by haukex (Archbishop) on May 16, 2020 at 20:38 UTC
|
| [reply] |