Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Trouble using setNodeText()

by Plankton (Priest)
on Apr 06, 2011 at 20:07 UTC ( #897873=perlquestion: print w/ replies, xml ) Need Help??
Plankton has asked for the wisdom of the Perl Monks concerning the following question:

Dear wise monks ... I pull out a couple of lists if ip address from my XML data
my $xp = XML::XPath->new(xml => $fpdomaininfo ); my $nodeset = $xp->find('//cidr-map'); foreach my $node ($nodeset->get_nodelist) { my $node_name = $node->findvalue ( '@name' ) -> stri +ng_value; my $ips_as_string = $node->string_value; ... make changes to ips_as_string ... }
Then I make changes to these list, then I want to put the data back into my xml data, but I cannot figure out what path I am suppose to use in the setNodeText call. I have tried this like ...
my $path = '//cidr-map[@name]='; $path .= "\"$node_name\"; $xp->setNodeText( $path, $ips_as_string );
... and ...
my $path = '//cidr-map/cidr-map-to[@name]='; $path .= "\"$node_name\"; $xp->setNodeText( $path, $ips_as_string );
... the xml looks like this ...
... <cidr-map name="Internal test" default-datacenter="Default Datacenter" +> <cidr-map-to name="AKA Edge"> <blocks>127.0.241.174 127.0.0.181</blocks> </cidr-map-to> </cidr-map> <cidr-map name="Prod Internal Actual" default-datacenter="Default Data +center"> <cidr-map-to name="AKA Edge"> <blocks>127.0.12.1 10.0.0.1 198.168.1.1</blocks> </cidr-map-to> </cidr-map> ...
.. and other ham fist-ed attempts while I grasp at straws. I cannot figure out for the life me what $path I should use in the setNodeText call. Can you all help? Thanks!

Comment on Trouble using setNodeText()
Select or Download Code
Re: Trouble using setNodeText()
by wind (Priest) on Apr 06, 2011 at 20:15 UTC
    Using XML::Twig, the below script takes your ip blocks and reverses their order:
    use XML::Twig; use strict; my $xml = do {local $/; <DATA>}; my $twig = XML::Twig->new( keep_spaces => 1, ); $twig->parse($xml); foreach my $node ($twig->findnodes(q{//cidr-map-to[@name='AKA Edge']/b +locks})) { $node->set_text(join ' ', reverse split ' ', $node->text()); } $twig->print(); __DATA__ <root> <cidr-map name="Internal test" default-datacenter="Default Datacenter" +> <cidr-map-to name="AKA Edge"> <blocks>127.0.241.174 127.0.0.181</blocks> </cidr-map-to> </cidr-map> <cidr-map name="Prod Internal Actual" default-datacenter="Default Data +center"> <cidr-map-to name="AKA Edge"> <blocks>127.0.12.1 10.0.0.1 198.168.1.1</blocks> </cidr-map-to> </cidr-map> </root>
Re: Trouble using setNodeText()
by Anonymous Monk on Apr 06, 2011 at 21:00 UTC
    . and other ham fist-ed attempts while I grasp at straws. I cannot figure out for the life me what $path I should use in the setNodeText call.

    Small program to test one function, like (this should probably be 3 different programs)

    #!/usr/bin/perl -- use strict; use warnings; use XML::XPath; Main( @ARGV ); exit( 0 ); sub Main { use XML::XPath; my $xc = XML::XPath->new(xml => SampleXML() ); print "ORIGINAL XML\n"; print $xc->findnodes_as_string('/'), "\n\n"; print "//blocks MATCHES MANY NODES \n"; $xc->setNodeText( '//blocks', ' blah blah ' ); print $xc->findnodes_as_string('/'), "\n\n"; print '//cidr-map-to[@name="AKA Edge"]/blocks MATCHES MANY NODES', + "\n"; $xc->setNodeText( '//cidr-map-to[@name="AKA Edge"]/blocks', ' aha +42 ' ); print $xc->findnodes_as_string('/'), "\n\n"; print '//cidr-map[2]/cidr-map-to/blocks MATCHES ONE NODE', "\n"; $xc->setNodeText( '/Product/cidr-map[2]/cidr-map-to/blocks', ' eur +eka ' ); $xc->setNodeText( '//cidr-map[2]/cidr-map-to/blocks', ' eureka ' ) +; $xc->setNodeText( '//cidr-map[@name="Prod Internal Actual"]/cidr-m +ap-to/blocks', ' eureka ' ); print $xc->findnodes_as_string('/'), "\n\n"; } sub SampleXML { return <<'__XML__'; <?xml version="1.0"?> <Product> <cidr-map default-datacenter="Default Datacenter" name="Internal tes +t"> <cidr-map-to name="AKA Edge"> <blocks>127.0.241.174 127.0.0.181</blocks> </cidr-map-to> </cidr-map> <cidr-map default-datacenter="Default Datacenter" name="Prod Interna +l Actual"> <cidr-map-to name="AKA Edge"> <blocks>127.0.12.1 10.0.0.1 198.168.1.1</blocks> </cidr-map-to> </cidr-map> </Product> __XML__ } __END__ ORIGINAL XML <Product> <cidr-map default-datacenter="Default Datacenter" name="Internal tes +t"> <cidr-map-to name="AKA Edge"> <blocks>127.0.241.174 127.0.0.181</blocks> </cidr-map-to> </cidr-map> <cidr-map default-datacenter="Default Datacenter" name="Prod Interna +l Actual"> <cidr-map-to name="AKA Edge"> <blocks>127.0.12.1 10.0.0.1 198.168.1.1</blocks> </cidr-map-to> </cidr-map> </Product> //blocks MATCHES MANY NODES <Product> <cidr-map default-datacenter="Default Datacenter" name="Internal tes +t"> <cidr-map-to name="AKA Edge"> <blocks> blah blah </blocks> </cidr-map-to> </cidr-map> <cidr-map default-datacenter="Default Datacenter" name="Prod Interna +l Actual"> <cidr-map-to name="AKA Edge"> <blocks> blah blah </blocks> </cidr-map-to> </cidr-map> </Product> //cidr-map-to[@name="AKA Edge"]/blocks MATCHES MANY NODES <Product> <cidr-map default-datacenter="Default Datacenter" name="Internal tes +t"> <cidr-map-to name="AKA Edge"> <blocks> aha 42 </blocks> </cidr-map-to> </cidr-map> <cidr-map default-datacenter="Default Datacenter" name="Prod Interna +l Actual"> <cidr-map-to name="AKA Edge"> <blocks> aha 42 </blocks> </cidr-map-to> </cidr-map> </Product> //cidr-map[2]/cidr-map-to/blocks MATCHES ONE NODE <Product> <cidr-map default-datacenter="Default Datacenter" name="Internal tes +t"> <cidr-map-to name="AKA Edge"> <blocks> aha 42 </blocks> </cidr-map-to> </cidr-map> <cidr-map default-datacenter="Default Datacenter" name="Prod Interna +l Actual"> <cidr-map-to name="AKA Edge"> <blocks> eureka </blocks> </cidr-map-to> </cidr-map> </Product>
    Read http://w3schools.com/xpath/default.asp for gentle introduction to xpath.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://897873]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (8)
As of 2014-08-30 09:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (291 votes), past polls