Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Perl - Modify the nested XML tags

by tobyink (Abbot)
on Jan 11, 2013 at 21:05 UTC ( #1012978=note: print w/ replies, xml ) Need Help??


in reply to Perl - Modify the nested XML tags

use strict; use warnings; use XML::LibXML 2; use XML::LibXML::PrettyPrint; my $xml = XML::LibXML::->load_xml(string => <<'INPUT'); <?xml version="1.0" encoding="UTF-8"?> <response> <result name="response" numFound="58582" start="0"> <doc> <str name="body">Have a great time at this park!</str> <int name="is_park_id">317851</int> <str name="ss_image_thumb_small"/> <str name="title">Playground</str> </doc> </result> </response> INPUT my %changes = ( '//response' => sub { $_->setNodeName('Fe +ed') }, '//result' => sub { $_->setNodeName('Pr +oducts'), %$_ = (); }, '//doc' => sub { $_->setNodeName('Pr +oduct'), %$_ = (); }, '//str[@name="body"]' => sub { $_->setNodeName('De +scription'), %$_ = (); }, '//int[@name="is_park_id"]' => sub { $_->setNodeName('Ex +ternalId'), %$_ = (); }, '//str[@name="ss_image_thumb_small"]' => sub { $_->setNodeName('Im +ageUrl'), %$_ = (); }, '//str[@name="title"]' => sub { $_->setNodeName('Na +me'), %$_ = (); }, ); while (my ($xpath, $processor) = each %changes) { $xml->findnodes($xpath)->foreach($processor); } print( XML::LibXML::PrettyPrint:: -> new(element => { compact => [qw/ Description ExternalId ImageUrl Name /], }) -> pretty_print($xml) -> toString );
perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'


Comment on Re: Perl - Modify the nested XML tags
Download Code
Re^2: Perl - Modify the nested XML tags
by Murari (Initiate) on Jan 14, 2013 at 15:19 UTC
    I have the these libraries installed in my machine, couldn't use XML::LibXML. XML::NamespaceSupport, XML::Parser, XML::RegExp, XML::SAX, XML::Simple My requirement is multi-register document, could you help in the solution for the input nested XML?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (12)
As of 2014-09-22 19:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (198 votes), past polls