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.)


In reply to XML::Simple Oddities and/or Philosophy by osunderdog

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.