Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Filter for XML elements

by tobyink (Abbot)
on Dec 13, 2012 at 09:02 UTC ( #1008638=note: print w/ replies, xml ) Need Help??


in reply to Filter for XML elements

Step 1 is: drop XML::Simple like a rock. If you continue along that route you're in for a world of pain. XML::Simple does have its uses, but what you're doing is not one of them.

XML::Twig (mentioned above) is specifically designed for the sort of task you're asking about - setting up a bunch of rules to handle incoming XML features and then streaming some XML through them.

Personally I'd generally use XML::LibXML - not because it's better than XML::Twig for this particular task but because it's the XML library I'm most familiar with. Here's how you could achieve your desired output using XML::LibXML...

use XML::LibXML 2; use XML::LibXML::PrettyPrint 'print_xml'; my $xml = XML::LibXML->load_xml(IO => \*DATA); # Promote <arit> elements out of their <z> container $xml -> findnodes('//z/arit') -> foreach(sub { $_->parentNode->parentNode->appendChild($_) }); # Remove certain elements $xml -> findnodes('//z | //runhd | //i-g | //zp_pvg | //zp_cl | //ar | +//zp_gr') -> foreach(sub { $_->parentNode->removeChild($_) }); print_xml $xml; __DATA__ <entry rootUID="281088" Class="A" Name="abc"><h-g><h>abc</h><runhd>abc +</runhd> <z>/</z><i-g><i>abc123</i></i-g><z>/</z></h-g><zp_pvg /><pv-g> <pv>abc is 123</pv><z> (</z><r r="REG" /><z><it2>Reg</it2>) </z> <d tranID="1" status="3">abc is a company.<chn localeUID="202" status= +"3" >(Chinese translation)</chn></d><z>: </z><x tranID="2" status="3" >abc launched in 1992.<chn localeUID="202" status="3" >Some other Chinese translation</chn></x><obj-g><zp_cl /> <z>Symbolmark</z><cl>rules</cl><ar>, </ar><cl>decision</cl><ar>, </ar> <cl>the law</cl><z>Another Symbol</z><syn>Something </syn><cf>(positiv +e)</cf> </obj-g><zp_gr /><z>some symbol</z><gr-g><gr gr="P" /><z><arit>1+1=3</ +arit> </z></gr-g></pv-g></entry>
perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'


Comment on Re: Filter for XML elements
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (12)
As of 2014-09-16 14:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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











    Results (26 votes), past polls