| Store | Cart

RE: Help with accessing an unknown set of data generated by XML::Simple

From: Grant McLean <gra...@mclean.net.nz>
Fri, 24 Feb 2012 13:33:58 +1300
On Thu, 2012-02-23 at 23:10 +0000, Dale Puckett wrote:
> 1. I need to figure out how to be able to put out the XML in the same> order contained in the Sample file as that is what the vendors expect.

You can't - it's that simple.  XML::Simple slings data back and forth
between XML and Perl hashes.  Hashes are unordered, so if you read some
XML, munge the hash and write out as XML the order will change.  If that
is a problem then don't use XML::Simple.

> 2. I having trouble wrapping my head around a way to access each node> and get its value when I do not know the name of the node. The example> Francisco provided does a great job pulling out a list of known nodes> that are presented in the @WANTED array, but I haven't figured out how> to generalize it so that I get all of the nodes, their names and> values when they exist.

If you're using XML::Simple then you work out what element names were in
the document by using Perl's builtin 'keys' function on each hash.  The
perlreftut man page might help there.

However I'd recommend not using XML::Simple (and I should know - I wrote
it).  I personally use XML::LibXML.  In the case where you know all the
element names then using XPath to go directly to the bits you're after
is probably easier than XML::Simple's nested hash approach.  For the
case where you don't know the element names then the DOM allows you to
iterate through the children of any element and ask each for its name.

For writing out XML, XML::Simple will cause you nothing but pain, and
XML::LibXML will probably be quite fiddly and time-consuming.  I have
had success with XML::Generator and also using a templating tool like
HTML::Mason or Template::Toolkit.  If you have the luxury of working
with some type of schema then there are other options which could make
your life simpler - I've never been in that position myself.

Regards
Grant


_______________________________________________
Perl-XML mailing list
Perl...@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Recent Messages in this Thread
Christopher R. Maden Feb 18, 2012 04:50 am
Francisco Obispo Feb 18, 2012 06:13 am
Dale Puckett Feb 23, 2012 11:10 pm
Francisco Obispo Feb 23, 2012 11:52 pm
Grant McLean Feb 24, 2012 12:33 am
Francisco Obispo Feb 24, 2012 12:41 am
Todd Rinaldo Feb 24, 2012 03:30 pm
Francisco Obispo Feb 24, 2012 04:31 pm
Todd Rinaldo Feb 24, 2012 04:35 pm
Dale Puckett Feb 18, 2012 02:35 am
Messages in this thread