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: XML::dom parsing complex xml problem
by Mark - BLS CTR Thomas other posts by this author
Jan 26 2005 12:31PM messages near this date
view in the new Beta List Site
Re: Contents of Perl-XML Digest, Vol 12, Issue 18 | xml::dom parsing complex xml problem
& XSLT Use XPath and it becomes a simple problem. 

Tested code:

#!/usr/bin/perl -w
use strict;
use XML::XPath;

my $xml = qq(
<Instructions> 
 	<Agent> 
 		<Contacts> 
 			<Contact> 
 				<TelephoneNumbers> 
  					<TelephoneNumber
Type="Mobile"> 777777</TelephoneNumber>
  					<TelephoneNumber
Type="Home"> 88888</TelephoneNumber>
  					<TelephoneNumber
Type="Work"> 99999</TelephoneNumber>
  				</TelephoneNumbers> 
  			</Contact> 
  		</Contacts> 
  		<Address/> 
		<TelephoneNumbers> 
  			<TelephoneNumber> 1111111</TelephoneNumber>
  		</TelephoneNumbers> 
  	</Agent> 
</Instructions> 
);

my $doc = XML::XPath-> new(xml=>$xml);

my @agents = $doc-> findnodes('//Agent');

foreach my $agent (@agents) {
    print $agent-> findvalue('TelephoneNumbers/TelephoneNumber'), "\n";
}

__END__

One cool thing about XPath is that it can turn any extraction into a one
liner. This is the above phone number extraction as a one-liner:

print join "\n", map {$_-> string_value}
$doc-> findnodes('//Agent/TelephoneNumbers/*');

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



>  -----Original Message-----
>  From: perl-xml-bounces@[...].com 
>  [mailto:perl-xml-bounces@[...].com] On Behalf 
>  Of Chris Oldfield
>  Sent: Wednesday, January 26, 2005 3:09 PM
>  To: perl-xml@[...].com
>  Subject: xml::dom parsing complex xml problem
>  
>  
>  I'm trying to parse a complex xml message, using XML::DOM for 
>  the first time, part of which has a format of
>  
>  <Instructions>
>   	<Agent>
>   		<Contacts>
>   			<Contact>
>   				<TelephoneNumbers>
>    					<TelephoneNumber 
>  Type="Mobile">777777</TelephoneNumber>
>    					<TelephoneNumber 
>  Type="Home">88888</TelephoneNumber>
>    					<TelephoneNumber 
>  Type="Work">99999</TelephoneNumber>
>    				</TelephoneNumbers>
>    			</Contact>
>    		</Contacts>
>    		<Address/>
>  		<TelephoneNumbers>
>    			<TelephoneNumber>1111111</TelephoneNumber>
>    		</TelephoneNumbers>
>    	</Agent>
>    </Instruction>
>  </Instructions>
>  
>  I can access the telephone numbers within Contact
>  
>  foreach my $Instruction($doc->getElementsByTagName('Instruction')){
>      foreach my $Agent($Instruction->getElementsByTagName('Agent')){
>            foreach my 
>  $Contact($Agent->getElementsByTagName('Contact')){
>              	foreach my 
>  $TelephoneNumber($Contact->getElementsByTagName('TelephoneNumber')){
>                  		print "\t\t\t\t",
>  $TelephoneNumber->getAttribute('Type'),": ",
>                      	
>  $TelephoneNumber->getFirstChild->getNodeValue(), "\n";
>              	}
>            }
>       }
>       foreach my 
>  $TelephoneNumber($Agent->getElementsByTagName('TelephoneNumber')){
>              print "\t\tTelephone Number: 
>  ",$TelephoneNumber->getFirstChild->getNodeValue(), "\n";
>       }
>  }
>  
>  The last loop returns not only the the telephone number 
>  relating to the agent but also those relating to contact. I 
>  under stand why it's doing this. How do I qualify the call to 
>  just return the telephone node(s) directly relating to 
>  <Agent> ignoring those within <Contact>?
>  
>  
>  
>  _______________________________________________
>  Perl-XML mailing list
>  Perl-XML@[...].com
>  To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
>  

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

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