RE: Help with accessing an unknown set of data generated by XML::Simple
Grant McLean <[email protected]> Fri, 24 Feb 2012 13:33:58 +1300
| Newsgroups | gmane.comp.lang.perl.xml |
|---|---|
| Message-ID | <1330043638.13962.34.camel@putnam> |
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 [email protected] To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs