Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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'

In reply to Re: Filter for XML elements by tobyink
in thread Filter for XML elements by uituit

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others having an uproarious good time at the Monastery: (4)
    As of 2018-05-25 23:27 GMT
    Find Nodes?
      Voting Booth?