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

In reply to Parsing XML - SAX events by ghouse

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.