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

I need to delete the first node of a XML file:
<xml> <answer> <url>http:....</url> <content>value...</content> </answer> <answer> <url>http:....</url> <content>value....</content> </answer> </xml>
I tried XML::DOM and XML::LibXML but there is no clear documentation to do it. Thanks in advance.

Replies are listed 'Best First'.
Re: delete first node XML
by tobyink (Canon) on Aug 13, 2012 at 00:20 UTC

    In the W3C XML DOM (which is implemented by XML::LibXML, and presumably by XML::DOM), to remove a node, you need to call the removeChild method on its parent node...

    use XML::LibXML 2.00; my $dom = XML::LibXML->load_xml(IO => \*DATA); my $answer = $dom->getElementsByTagName('answer')->get_node(1); $answer->parentNode->removeChild($answer); print $dom; __DATA__ <xml> <answer> <url>http:....</url> <content>value 1</content> </answer> <answer> <url>http:....</url> <content>value 2</content> </answer> </xml>
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: delete first node XML
by GrandFather (Saint) on Aug 12, 2012 at 23:38 UTC

    So show us the code you tried and tell us how it failed for you.

    True laziness is hard work