lib::libXML parsing comments

Discussion in 'Perl Misc' started by Fergus McMenemie, Jan 19, 2012.

  1. 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:)
     
    Fergus McMenemie, Jan 19, 2012
    #1
    1. Advertising

  2. 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 <> wrote:

    > Quoth (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
     
    Fergus McMenemie, Jan 20, 2012
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ian Gregory
    Replies:
    1
    Views:
    533
  2. Olav
    Replies:
    3
    Views:
    4,372
  3. jwang

    libxml: Parsing XML Question?

    jwang, Jul 6, 2004, in forum: C Programming
    Replies:
    5
    Views:
    427
    TLOlczyk
    Jul 7, 2004
  4. Jon Smirl

    Parsing xhtml with libxml

    Jon Smirl, Dec 16, 2005, in forum: Ruby
    Replies:
    1
    Views:
    173
    Eero Saynatkari
    Dec 16, 2005
  5. subimage
    Replies:
    11
    Views:
    357
    Mathieu Blondel
    Jun 8, 2006
Loading...

Share This Page