ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> perl-xml
perl-xml
RE: simple XML::XPath question
by Mark - BLS CTR Thomas other posts by this author
Jul 14 2006 3:28PM messages near this date
view in the new Beta List Site
simple XML::XPath question | Re: simple XML::XPath question
& XSLT Lev Lvovsky wrote:
>  I'm new to proper XML parsing (read: not a one-off regex) - 
>  I've gone through the XPath tutorial on w3schools.com, but 
>  have a question in regards to usage of XML::XPath in particular
>  
>  say that this is my xml file which I'd like to parse:
...
>  What I'd like to do, is get all this stuff into a hash like this one:
...
>  I would think that I could do this with a series of for 
>  loops,

You can do much better than that! All you need is one loop.

>  my @DataLists = $xp->findnodes("/CompanyData/Data/Datalist);

This is a typical newbie mistake. You've stopped too early. You're not
really doing anything with the DataList element itself, right? It's the
children you're interested in. Go straight to them!

What you really want are the DataListElementX nodes, or put another way,
the children of DataList following the Id. So that's what we'll loop
over:

foreach my $elem ($x-> findnodes('//DataList/*[preceding-sibling::Id]')){

  # Get the Id which is the key to the hash
  my $Id = $elem-> findvalue('preceding-sibling::Id');

  # Now push it on to the hash as an array element
  push @{$DataList{$Id}}, $elem-> string_value;
}

That's all you need to do. This is the result:

%DataList = (
  'first' =>  [' first_element_1 ',' ... ',' ... '],
  'second' =>  [' second_element_1 ',' ... ',' ... '],
  'third' =>  [' ... ',' ... ',' ... ']
);

-- 
Mark Thomas 
Internet Systems Architect
_______________________________________
BAE SYSTEMS Information Technology 
2525 Network Place
Herndon, VA  20171  USA 

_______________________________________________
Perl-XML mailing list
Perl-XML@[...].com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
Thread:
Lev Lvovsky
Mark - BLS CTR Thomas
Yanick Champoux
Lev Lvovsky

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved