ghouse has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I need to read a attribute value in an XML of the As I am new to XML, I am having trouble on this ...Can you help me in this Sample XML is as following
<?xml version="1.0" encoding="ISO-8859-1" ?> - <RESEARCH> <xslobject name="research.xsl" /> <PGFROM>12/30/04</PGFROM> <PGOFFSET>0</PGOFFSET> <PGCOUNT>1</PGCOUNT> - <LINK PUBID="584557" DOCID="100386419" CLIENT="0" HIDDEN="1" DELETED +="0"> <SCORE>1.00</SCORE> <PUBDATE>01/06/05</PUBDATE> <DOCDATE>01/06/05</DOCDATE> <EXPDATE>NEVER</EXPDATE> <TITLE>SC 4 DOC AND IMAGE</TITLE> <URI>/DDL/servlets/dv.search?docID=100386419&searchType=INTRANET</UR +I> <FILETYPE>doc</FILETYPE> </LINK> </RESEARCH>
I am able to read an Element but to read an attrtibute say the following line in XML " <LINK PUBID="584557" DOCID="100386419" CLIENT="0" HIDDEN="1" DELETED="0"> " How do I do it ... My sample code is
sub ParseCSrsp { my ($CSrspXMLMsg,$StatusRef) = @_; my $CSxmlParser; $CSxmlParser = new XML::Parser(ErrorContext => 2); $CSxmlParser->setHandlers(Init => \&InitHandler, Final => \&FinalHandler, Start => \&StartHandler, End => \&EndHandler, Char => \&CharHandler); $CSxmlParser->parse($CSrspXMLMsg); return 1; } # XML parser handler functions sub InitHandler { $CSElement = {}; undef $XMLTagContent; } sub FinalHandler { } sub StartHandler { my $Element = $_[1]; undef $XMLTagContent; &{$LBSubIndex{$Element}} if $LBSubIndex{$Element}; if ($Element =~ /URI/i) { $TagElement = 'E'; } } sub EndHandler { my $Element = $_[1]; # &{$LBSubIndex{$Element . '_'}} if $LBSubIndex{$Element . '_'}; &{$LBSubIndex{$Element}} if $LBSubIndex{$Element}; } sub CharHandler { $XMLTagContent .= $_[1]; } # Custom handlers sub ParseELEMENT_ { $CSElement = {}; } sub LBGeneric { my $Element = $_[1]; print "$Element is generic element and $XMLTagContent is tagcon +tent\n"; if ($TagElement eq 'E') { if ($XMLTagContent eq '') { $PrevContact = ' '; } else { $PrevContact = $XMLTagContent ; } print "GOT IT NOW $PrevContact\n"; $TagElement = ''; $URL = $PrevContact; } elsif ($XMLTagContent =~ /search/i) { print "GOT IT NOW $PrevContact\n"; } # $CSrsp->{$Element} = $XMLTagContent if $XMLTagContent; } # Custom handlers sub ParseELEMENT_ { $CSElement = {}; print $CSElement; }
Thanks, Ghouse

Replies are listed 'Best First'.
Re: Parsing XML - SAX events
by gellyfish (Monsignor) on Jan 06, 2005 at 13:48 UTC

    If all you are interested in is the value of a single attribute then you might be better off using XPath:

    use XML::XPath + $/ = undef; + my $foo = <DATA>; my $xp = XML::XPath->new(xml => $foo); + my $nodes = $xp->find('/RESEARCH/LINK/@PUBID'); + foreach my $node ($nodes->get_nodelist()) { print $node->getData(),"\n"; } __END__ <RESEARCH> <xslobject name="research.xsl" /> <PGFROM>12/30/04</PGFROM> <PGOFFSET>0</PGOFFSET> <PGCOUNT>1</PGCOUNT> <LINK PUBID="584557" DOCID="100386419" CLIENT="0" HIDDEN="1" DELETED=" +0"> <SCORE>1.00</SCORE> <PUBDATE>01/06/05</PUBDATE> <DOCDATE>01/06/05</DOCDATE> <EXPDATE>NEVER</EXPDATE> <TITLE>SC 4 DOC AND IMAGE</TITLE> <URI>/DDL/servlets/dv.search?docID=100386419&amp;searchType=INTRANET +</URI> <FILETYPE>doc</FILETYPE> </LINK> </RESEARCH>
    (BTW, Your XML is not well formed - you need to render the ampersand in the URI as the entity &amp;)

    /J\

Re: Parsing XML - SAX events
by mirod (Canon) on Jan 06, 2005 at 14:40 UTC

    You are using XML::Parser, which brings 2 comments: first it is NOT a SAX parser, it has its own onterface, which actually predates the SAX spec. Secondly: don't use XML::Parser! It is a very useful low-level module, but not the easiest to use.

    Lots of nicer modules have been built on top of XML::Parser (or on top of other parsers) that will make your life much easier. In your case it looks like you could use XML::Simple. If you want SAX, then XML::SAX will do. Other options include XML::XPath, as mentioned by gellyfish, XML::LibXML, based on libxml2, and (of course ;--) XML::Twig.

    Finally, if you are new to XML in Perl, you should probably read the Perl XML FAQ.

Re: Parsing XML - SAX events
by holli (Abbot) on Jan 06, 2005 at 12:49 UTC
    sub StartHandler { my $expat = shift; my $element = shift; my %attr = @_; print $attr{PUBID}; #... }
      Thank you . But I am not sure as to how do i capture this value and take it store it in a global variable which can be accessed later