Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

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'

Replies are listed 'Best First'.
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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1012978]
[jedikaiti]: Hi, Monks
[marto]: heya jedikaiti!
[jedikaiti]: Cute Robot!
[jedikaiti]: How's life, marto?
[marto]: I saw this robot the other day, pretty fancy stuff. Things are pretty good jedikaiti, improving :) How are things with you?
[jedikaiti]: Doing OK, but a little achy this AM - I think the cats were using me as a wrestling mat and mattress while I was asleep last night.

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (17)
As of 2017-07-25 15:04 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (374 votes). Check out past polls.