osunderdog has asked for the wisdom of the Perl Monks concerning the following question:
The other day I ran into a situation where I wanted to add an xml node to an existing xml structure. Initially, I chose to insert the node using XML::Simple. The key was that I needed to ensure that I didn't change the XML structure of the original, other than the addition of a single node. However I quickly realized that the XML structure of the output was not the same as the input to XMLin.
Here is my sample:
use strict; use warnings; use XML::Simple; my $filename = "sample_file.xml"; my $xs = XML::Simple->new(keeproot=>1, forcearray=>1); my $xmlString; { $/++; $xmlString = <DATA>; } my $data_hr = $xs->XMLin($xmlString); my $resultXMLString = $xs->XMLout($data_hr); print "$resultXMLString\n"; ## ## RESULT: ## ##<a> ## <FOOFOOFOOFOO> ## <m>foofoo 44444</m> ## <pm>barbarbar222</pm> ## <mfg someid="2345">xyzzy</mfg> ## <r>nn23v</r> ## <s>555</s> ## </FOOFOOFOOFOO> ##</a> ## ## ## __DATA__ <a id="FOOFOOFOOFOO"> <s>555</s> <m>foofoo 44444</m> <pm>barbarbar222</pm> <r>nn23v</r> <mfg someid='2345'>xyzzy</mfg> </a>
Notice that the a node has an attribute on input, however it does not have one on the result. On the other hand, the attribute of node mfg is symmetrical.
With a deadline looming, I ended up using XML::LibXML to read the xml into DOM and then add the node.
This leads to my philosophical question:
Is this where XML::Simple breaks down and I should opt to use XML::LibXML instead? The documentation for XML::Simple alludes to this by saying that
If your needs are not so simple, this may not be the mod ule for you. In that case, you might want to read the section on "WHERE TO FROM HERE?".
I guess what I wanted to do was pretty simple....
Also, after reviewing this situation (for this posting) I have further discovered that the behaviour I see is only related to the root node. If the root node has an attribute, it is not translated back to an attribute for the root node.
For example, the following code works as I expect, however the node I want to modify isn't the top node:
use strict; use warnings; use XML::Simple; my $filename = "sample_file.xml"; my $xs = XML::Simple->new(keeproot=>1, forcearray=>1); my $xmlString; { $/++; $xmlString = <DATA>; } my $data_hr = $xs->XMLin($xmlString); my $resultXMLString = $xs->XMLout($data_hr); print "$resultXMLString\n"; ## ## RESULT: ## ##<someotherroot> ## <a name="FOOFOOFOOFOO"> ## <m>foofoo 44444</m> ## <mfg someid="2345">xyzzy</mfg> ## <pm>barbarbar222</pm> ## <r>nn23v</r> ## <s>555</s> ## </a> ##</someotherroot> ## ## ## __DATA__ <someotherroot> <a id="FOOFOOFOOFOO"> <s>555</s> <m>foofoo 44444</m> <pm>barbarbar222</pm> <r>nn23v</r> <mfg someid='2345'>xyzzy</mfg> </a> </someotherroot>
Hazah! I'm Employed! (But not necessarily happily employed.)
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: XML::Simple Oddities and/or Philosophy
by runrig (Abbot) on Sep 26, 2005 at 20:08 UTC | |
by osunderdog (Deacon) on Sep 26, 2005 at 20:25 UTC | |
Re: XML::Simple Oddities and/or Philosophy
by izut (Chaplain) on Sep 26, 2005 at 20:13 UTC | |
Re: XML::Simple Oddities and/or Philosophy
by planetscape (Chancellor) on Sep 27, 2005 at 06:09 UTC | |
Re: XML::Simple Oddities and/or Philosophy
by leriksen (Curate) on Sep 27, 2005 at 07:03 UTC | |
by osunderdog (Deacon) on Sep 28, 2005 at 13:23 UTC | |
Re: XML::Simple Oddities and/or Philosophy
by grantm (Parson) on Sep 28, 2005 at 00:26 UTC |