Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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?




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>


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/ 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

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

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (8)
As of 2023-03-31 15:58 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (76 votes). Check out past polls.