Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Entity error in XML::XPATH

by cognizant (Pilgrim)
on Mar 03, 2006 at 11:19 UTC ( [id://534193]=perlquestion: print w/replies, xml ) Need Help??

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

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"; }

Replies are listed 'Best First'.
Re: Entity error in XML::XPATH
by PodMaster (Abbot) on Mar 03, 2006 at 11:28 UTC
    I got parsing error b'cos of entities.
    What error message (important, copy/paste)?
    I'm sure that there is a better way to do this. Could u pls suggest me the better way?
    Without seeing sample input and an error message (How (Not) To Ask A Question), the best answer you can hope for is to "fix" your XML so its properly formed :/

    update: XML::Parser undefined entity error might interest you.

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

Re: Entity error in XML::XPATH
by Aristotle (Chancellor) on Mar 03, 2006 at 21:06 UTC

    That document is not well-formed XML. Named entities like &Eacute; have to be defined in a DTD, but you did not supply any, so the parser correctly complains.

    Makeshifts last the longest.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://534193]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (7)
As of 2024-03-28 10:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found