removing comments from XML using Perl XML::DOM

Discussion in 'XML' started by user@domain.invalid, Jul 15, 2004.

  1. Guest

    Hi

    How do i remove comments from a DOM object ?

    I tried searching the man pages XML::DOM, XML::DOM::Comment and
    XML::DOM::Document but could not find anything

    The code used to parse the file is below ..

    use strict;
    use warnings;

    use XML::DOM;

    my $file = shift ;
    my $parser = new XML::DOM::parser;
    my $doc = $parser->parsefile ($file);

    # How do I remove the comments from $doc ?


    TIA
    AB
     
    , Jul 15, 2004
    #1
    1. Advertising

  2. In comp.lang.perl.misc lid wrote:
    > Hi
    >
    > How do i remove comments from a DOM object ?
    >
    > I tried searching the man pages XML::DOM, XML::DOM::Comment and
    > XML::DOM::Document but could not find anything
    >


    I'm not sure that XML::DOM is the way to go here you may be better off
    using XML::Twig or XML::SAX, but as you asked:


    use strict;
    use warnings;

    use XML::DOM;

    my $file = shift;
    my $parser = new XML::DOM::parser;
    my $doc = $parser->parsefile($file);

    remove_comments($doc);

    print $doc->toString();

    sub remove_comments {
    my ($doc) = @_;
    foreach my $node ( $doc->getChildNodes() ) {
    if ( $node->getNodeType() == ELEMENT_NODE ) {
    remove_comments($node);
    }
    elsif ( $node->getNodeType() == COMMENT_NODE ) {
    $doc->removeChild($node);
    }
    }
    }

    This comes with the caveat that I have only tested it with limited data.

    /J\
     
    Jonathan Stowe, Jul 15, 2004
    #2
    1. Advertising

  3. Tom Regner Guest

    lid wrote:

    > Hi
    >
    > How do i remove comments from a DOM object ?
    >

    tested, works, you need a few more moudles though:

    code:
    ----------------------------------
    use XML::DOM;
    use XML::XQL;
    use XML::XQL::DOM;

    my $file = shift ;
    my $parser = new XML::DOM::parser;
    my $doc = $parser->parsefile ($file);
    my @comments = $doc->xql('//comment()');
    for my $node (@comments) {
    my $parent = $node->getParentNode();
    $parent->removeChild($node);
    }
    $doc->printToFile ("out.xml");
    __END__
    1) code speichern als xql.pl

    2 Datei anlegen: test.xml
    <xml>
    <test></test>
    <!-- comment 1 -->
    <test><!-- comment 2 -->
    </test>
    </xml>

    3) ausf├╝hren:
    [1256]tom@margo perl $ perl ./xql.pl test.xml

    4) Ausgabe (out.xml):
    <xml>
    <test/>

    <test>
    </test>
    </xml>
    ----------------------------------

    hth,
    Tom Regner
    --
    Dievision GmbH | Kriegerstrasse 44 | 30161 Hannover
    Telefon: (0511) 288791-0 | Telefax: (0511) 288791-99
    http://www.dievision.de | mailto:
     
    Tom Regner, Jul 15, 2004
    #3
    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. Replies:
    0
    Views:
    1,131
  2. Monk
    Replies:
    10
    Views:
    1,475
    Michael Wojcik
    Apr 20, 2005
  3. Brendan Byrd/SineSwiper

    Removing Perl comments and strings using regexps

    Brendan Byrd/SineSwiper, Jul 17, 2003, in forum: Perl Misc
    Replies:
    4
    Views:
    152
    Brendan Byrd/SineSwiper
    Jul 18, 2003
  4. Replies:
    3
    Views:
    125
  5. Replies:
    2
    Views:
    103
Loading...

Share This Page