in reply to Problem parsing XML into arrays using XML::Simple

I would tackle that with XML::LibXML and use XPath to access the data you want. Here's how to grab the URLs out of the FragmentAssets and SiteAssets nodes.

use strict; use warnings; use XML::LibXML; use Data::Dumper; my $parser = XML::LibXML->new({"encoding" => "utf-8"}); my $doc = $parser->parse_file("pm884843.xml"); my @paths = ( "/SiteStudioManifest/FragmentAssets/asset", "/SiteStudioManifest/SiteAssets/asset" ); for my $path (@paths) { my @url = map { $_->getAttributeNode("url")->getValue() } $doc->fi +ndnodes($path); print "URLs for $path:\n"; print "\t$_\n" for (@url); print "\n"; }
Output:
C:\autoworking\perl>perl pm884843.pl URLs for /SiteStudioManifest/FragmentAssets/asset: /ucm/fragments/web_header/css/nav_ie6fix.css /ucm/fragments/css4footer/img/promotions/transpare +ncy.png /ucm/fragments/css4footer/img/bg-box-menu.gif URLs for /SiteStudioManifest/SiteAssets/asset: /ucm/groups/public/@websitestructure/documents/file/pghdr_left +.jpg

You could grab the HttpSiteAddress attribute value from the root SiteStudioManifest node in a similar fashion.

Replies are listed 'Best First'.
Re^2: Problem parsing XML into arrays using XML::Simple
by locked_user sundialsvc4 (Abbot) on Jan 28, 2011 at 17:30 UTC

    I very strongly agree with this approach.   You see, you actually have a very well-known and universal problem:   you need to fetch particular well-known things out of an XML data structure.   You want to be able to do that generically, without creating (and having to constantly maintain) complex procedural code to do so.   “XPath expressions” are a perfect way to do that.

    So, say goodbye and good day (in this particular case) to XML::Simple.   Time to call in his big-brother.

    In fact, another technology that you might wish to look into is “XML style sheets (XSL),” which allow you to specify complete XML extractions and transformations, non-procedurally.   While the technology is, shall we say, “somewhat obfuscatory,” it is nonetheless very powerful and therefore worth study.   I need not bother to say that Perl has excellent support for it.   Of course it does... of course...

Re^2: Problem parsing XML into arrays using XML::Simple
by tevolo (Novice) on Jan 28, 2011 at 15:48 UTC

    Wow, thanks for that code. As is always the case minutes after posting to this site I came across a solution that works but now I will also try your code as well to see which works best. Thanks again

    This is what I found to work

    use LWP::Simple; use XML::Simple; use Data::Dumper; use strict; use warnings; my $xml = new XML::Simple (KeyAttr=>[]); my $parser = $xml->XMLin("c:\\temp\\data.xml"); print Dumper($parser); foreach my $e (@{$parser->{FragmentAssets}->{asset}}) { print $e->{url}, "\n"; print "\n"; }