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 Chris Oldfield other posts by this author
Jan 26 2005 1:13PM messages near this date
view in the new Beta List Site
Re: xml::dom parsing complex xml problem | RE: xml::dom parsing complex xml problem
& XSLT Grant, Overwhelmed by the speedy response, thanks.

I take your point about speed. I asked the same questions of Mark:

How do I access the attributes (including the ones within <Agent>  not shown
in my example)?
Can you recommend a good tutorial site.

Thanks again,

Chris

-----Original Message-----
From: Grant McLean [mailto:grant@[...].nz]
Sent: 26 January 2005 20:54
To: Chris Oldfield
Cc: perl-xml@[...].com
Subject: Re: xml::dom parsing complex xml problem


On Wed, 2005-01-26 at 20:09 +0000, Chris Oldfield wrote:
>  I'm trying to parse a complex xml message, using XML::DOM for the first
>  time, part of which has a format of

It's probably not a good idea to write new code using XML::DOM

  http://perl-xml.sourceforge.net/faq/#xml_dom

As Mark pointed out, you want a DOM module that supports XPath.  His
example used XML::XPath which you should be able to install easily since
it has the same dependencies as XML::DOM.

Another option would be XML::LibXML which has a similar API but is
faster since the DOM is implemented in C rather than Perl.  Here's a
LibXML example:

#!/usr/bin/perl -w

use strict;

use XML::LibXML;

my $parser = XML::LibXML-> new();

my $doc  = $parser-> parse_fh(\*DATA);
my $root = $doc-> getDocumentElement;

foreach my $agent ($root-> findnodes('/Instructions/Instruction/Agent'))
{
  my @phone_no =
$agent-> findnodes('./TelephoneNumbers/TelephoneNumber');
  print $phone_no[0]-> to_literal, "\n";
}

exit;

__DATA__
<Instructions> 
  <Instruction> 
    <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> 


Another option would be XML::Twig which has some XPath support too.

Cheers
Grant



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

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