alienhuman has asked for the wisdom of the Perl Monks concerning the following question:

Hey Monks,

I'm using XML::Simple to slurp in, spit out xml for a project that's just in the prototype phase. I'm using the forcearray option as recommended, but I'm noticing something strange that I haven't been able to figure out by looking at the XML::Simple documentation (not saying it ain't in there, just that I can't seem to figure out where).

I'm starting with an xml document that looks like this:

<xml> <class instructor="young" subject="social studies" level="7" id="rm1 +02"> <student id="1"> <first_name>Francis</first_name> <last_name>Bustardo</last_name> </student> <student id="2"> <first_name>Escarlet</first_name> <last_name>Testamente</last_name> </student> <student id="3"> <first_name>Franco</first_name> <last_name>Coolamente</last_name> </student> </class> </xml>

And I'm using XMLin to slurp it in, passing the hashref to XMLout (calling with no options) and outputting the following xml as a result:

<opt> <class level="7" instructor="young" subject="social studies" name="r +m102"> <student name="1"> <first_name>Francis</first_name> <last_name>Bustardo</last_name> </student> <student name="2"> <first_name>Escarlet</first_name> <last_name>Testamente</last_name> </student> <student name="3"> <first_name>Franco</first_name> <last_name>Coolamente</last_name> </student> </class> </opt>

So my questions are:
-why is XML::Simple changing my <xml> to <opt>?
-why is it changing <student id="number"> to <student name="number">?

Thanks for any advice,

AH

p.s. I'm pretty new to xml and it's rules... I've been looking at the hash created using Data::Dumper and see that the resultant data structure doesn't retain the "id" name... can anyone help me figure out how to save that?

Replies are listed 'Best First'.
Re: XML::Simple::XMLout() renaming tags
by Aristotle (Chancellor) on Oct 02, 2003 at 21:46 UTC

    XML::Simple doesn't store any information on the input data; whichever things don't map to a representation in the Perl data structure will not be present if you use the same structure to create XML from. Since XMLin produces an anonymous array or hash, it has no way of storing the root element's name, so the same structure fed to XMLout will be rendered with a default root element name of opt.

    For the id/name confusion, check the XML::Simple docs for the KeyAttr option.

    You may be interested in reading Does your XML::Simple code pass the strict test? as well.

    Makeshifts last the longest.

      To modify the root element name...
      XMLout($structure, rootname => 'enter_rootname_here');
      cheers, dextius
Re: XML::Simple::XMLout() renaming tags
by Cody Pendant (Prior) on Oct 02, 2003 at 21:35 UTC
    I had a question like this a while ago, I'm not sure if it answers all your questions but take a look at this thread


    ($_='kkvvttuubbooppuuiiffssqqffssmmiibbddllffss') =~y~b-v~a-z~s; print
Re: XML::Simple::XMLout() renaming tags
by vadim_t (Acolyte) on Oct 04, 2003 at 16:22 UTC
    I'm not sure if this helps or not, but according to my book, anything beginning with "xml" in lower, upper, or mixed case is reserved. So you should replace "<xml>" with something different.