Monks,

I wanna extract specific elements from the xml file (Example: h1/title). I have tried using XML:XPATH as follows:

my $str = $ARGV[0]; my $xp = XML::XPath->new(xml => "$str"); my $nodeset = $xp->find('//h1/title'); # foreach my $node ($nodeset->get_nodelist) {my $tag=XML::XPath::XMLParser::as_string($node); print "$tag\n";}

I got parsing error b'cos of entities. Hence i've replaced all the entities with some other characters and after extracting all the tags i've replaced back the original entites. I got the result which i need.

I'm sure that there is a better way to do this. Could u pls suggest me the better way?

Cheers

--C

Update:

sample xml file

<xml> <h1><title>TH&Eacute;MATIC INTRODUCTION</title> <p>Para goes here</p> <h2><title>Sub level title</title> <p>Para Text</p></h2> </h1></xml>

Error:

undefined entity at line 2, column 13, byte 19: <xml> <h1><title>TH&Eacute;MATIC INTRODUCTION</title> ============^ <p>Para goes here</p> <h2><title>Sub level title</title> at c:/Perl/site/lib/XML/Parser.pm line 187

Perl code

use XML::Parser; use XML::Parser::Expat; use XML::Twig; use XML::XPath; undef $/; $file = $ARGV[0]; open (IN, "<$file"); $xml = <IN>; my $xp = XML::XPath->new(xml => "$xml"); my $nodeset = $xp->find('//h1/title'); # foreach my $node ($nodeset->get_nodelist) { my $tag=XML::XPath::XMLParser::as_string($node); print "$tag\n"; }

In reply to Entity error in XML::XPATH by cognizant

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.