Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

LibXML error when trying to delete a node in DOM

by msivask (Initiate)
on Apr 26, 2019 at 10:49 UTC ( [id://1233012]=perlquestion: print w/replies, xml ) Need Help??

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

Hi ,

Below is my requirement

I have an XML like

<sys xmlns="urn:mavenir:ns:yang:wrg-sc:6.0.2.8"> <platform><contact> <name>Mavenir</name> <phone>214-616-3499</phone> </contact> <solution> <name>mOne</name> <fkcontact>Mavenir</fkcontact> </solution> <tmmTableFilenameMap> <fileName>ALARM</fileName> <tableId>1</tableId> </tmmTableFilenameMap> <tmmTableFilenameMap> <fileName>AVGOVLDPERF</fileName> <tableId>7</tableId> </tmmTableFilenameMap> </platform> </sys>

I want to delete all nodes with tmmTableFilenameMap

I tried the below code

#!/usr/bin/perl use 5.010; use strict; use warnings; use XML::LibXML; my $filename = 'webrtcc_system_static_config_data_platremoved.xml'; my $a; my $dom = XML::LibXML->load_xml(location => $filename); my $book = $dom->documentElement; my $del="tmmTableFilenameMap"; my @a = $book->findnodes("tmmTableFilenameMap"); $book->removeChild($a); print $book->serialize();

It gives a strange error as below

XML::LibXML::Node::removeChild() -- node is not a blessed SV reference + at DeleteNodesXml.pl line 17.

Could some one help on this

2019-04-26 Athanasius added code and paragraph tags

Replies are listed 'Best First'.
Re: LibXML error when trying to delete a node in DOM
by hippo (Bishop) on Apr 26, 2019 at 11:25 UTC
    $book->removeChild($a);

    It's a bit hard to tell because you haven't used <code> tags around your code, but I don't see where you are assigning any value to $a so it is probably undef when you try to use it as an argument to removeChild().

    Also, please don't use $a as a variable name - it has a special meaning for sort and can bite you if you are not careful.

      after the ergonomic intervention of monk Athanasius, I can also see the use of $a instead of $a[0].

Re: LibXML error when trying to delete a node in DOM
by choroba (Cardinal) on Apr 26, 2019 at 15:31 UTC
    Your XML document has a XML namespace defined. Use XML::LibXML::XPathContext to handle namespaces in XML::LibXML.

    Also, the correct XPath expression to find tmmTableFilenameMap anywhere in the document is //tmmTableFilenameMap, otherwise the searching only happens at the current node.

    #! /usr/bin/perl use strict; use warnings; use XML::LibXML; my $filename = shift; my $xpc = 'XML::LibXML::XPathContext'->new; $xpc->registerNs(m => 'urn:mavenir:ns:yang:wrg-sc:6.0.2.8'); my $dom = 'XML::LibXML'->load_xml(location => $filename); for my $delete ($xpc->findnodes('//m:tmmTableFilenameMap', $dom)) { my ($parent) = $delete->findnodes('..'); $parent->removeChild($delete); } print $dom->serialize;

    It gets even shorter using XML::XSH2:

    open webrtcc_system_static_config_data_platremoved.xml ; register-namespace m urn:mavenir:ns:yang:wrg-sc:6.0.2.8 ; delete //m:tmmTableFilenameMap ; save :b ;

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re: LibXML error when trying to delete a node in DOM
by Jenda (Abbot) on May 02, 2019 at 17:19 UTC
    use strict; use XML::Rules; my $rules = XML::Rules->new( style => 'filter', rules => {tmmTableFilenameMap => sub {}}, namespaces => {'urn:mavenir:ns:yang:wrg-sc:6.0.2.8' => ''} ); $rules->filter(\*DATA); __DATA__ <sys xmlns="urn:mavenir:ns:yang:wrg-sc:6.0.2.8"> ...

    This doesn't load the whole file into memory which may be a big advantage.

    Jenda
    Enoch was right!
    Enjoy the last years of Rome.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2024-04-20 03:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found