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'
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.