Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
use strict; use XML::Rules; my $parser = XML::Rules->new( rules => { _default => '', 'tags,to,keep' => 'raw', company => sub {$_[0] => $_[1]}, }, style => 'filter', ); $parser->filter(\*DATA); __END__ <root> <other><some>blah</some>foo</other> <company> <tags>xxx</tags> <tags>yyy</tags> <skip x="1">aaa</skip> </company> <company name="PerlSoft"> <tags>xxx</tags> <tags>yyy</tags> <skip x="1">aaa</skip> </company> </root>

Change the list of tags to keep and the name of the repeated company tag to whatever you need and you should be done ;-)

As you can see, the filtering is done only under the <company> tag, if you need to do it inside several tags, just specify their names separated by commas, just like the list of tags to accept. Keep in mind though that while processing the file the data inside each of the specified tags will be accumulated in case the rule (the anonymous subroutine) needed to make changes to it. So the contents of those tags should fit easily in memory. That's why I did not specify the subroutine rule for the root tag, but rather for the individual company.

Update: Of course you can do the insert(s)/update(s) at the same time as the filtering. You'd just have to specify what data do you want from what tag and how to include it in the datastructure being built and either copy them to the database in the rule for the company tag or even insert them once each tag that maps to a table is fully parsed and replace the data of that tag by just the ID to be used when copying the parent tag.


In reply to Re: Easily XML filtering? by Jenda
in thread Easily XML filtering? by mattr

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



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2024-04-25 13:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found