lib::libXML parsing comments

F

Fergus McMenemie

hi,

I have to parse an XML document and rewrite it, after sorting some of
the nodes. Some of these nodes have assocated comments which I have been
told have to remain beside their node.

However walking the list of nodes returned by XML::LibXML's
getChildNodes or childNodes I never see XML_COMMENT_NODE returned. It
looks as though the parser is discarding comments.

perl : v5.8.8
XML::LibXML : 1.7

Comments please:)
 
F

Fergus McMenemie

Hummm, I had cancelled this message but I guess either cancelling did
not work or I was not quick enough. However everything works fine. I
had not properly accounted for how the comment was nested within my
documents structure.

Sorry for wasting your time. But on the other hand your example is
lovely.


en Morrow said:
Quoth (e-mail address removed) (Fergus McMenemie):
I have to parse an XML document and rewrite it, after sorting some of
the nodes. Some of these nodes have assocated comments which I have been
told have to remain beside their node.

However walking the list of nodes returned by XML::LibXML's
getChildNodes or childNodes I never see XML_COMMENT_NODE returned. It
looks as though the parser is discarding comments.

Works for me:

#!/opt/perl/bin/perl

use 5.010;
use warnings;
use strict;
use XML::LibXML;

my $X = XML::LibXML->load_xml(IO => \*DATA);
say sprintf "[%s] [%s]", $_->nodeName, $_->textContent
for $X->documentElement->childNodes;

__END__
<?xml version="1.0"?>

<xml>
<!-- comment -->
<element/>
</xml>

gives

[#text] [
]
[#comment] [ comment ]
[#text] [
]
[element] []
[#text] [
]

Can you post a miminal example which doesn't do what you want?

Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top