Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

The easiest and best known way to parse a small XML file into a Perl datastructure is to use XML::Simple like this.

use XML::Simple; my $ref = XMLin( $xmlfile);

The catch is that as soon as the XML does not look all the same in all places, you get a structure that's hard to work with. Try to look at the structure you get by parsing this:

<root> <sub> <a>foo</a> <a>bar</a> <b>blah</b> </sub> <sub> <a>baz</a> <b x="4">blah blah</b> </sub> </root>

In the first element of the {sub} array the {a} is an arrayref, in the second it's a string. The {b} on the other hand is a string in the first element and a hash in the second. So if you want to get the content of the <b> tag you have to check whether it's a hashref or the string you want.

The first problem may be solved by the ForceArray option for XMLin(), the second could be solved by ForceContent if only you could specify what tags does it affect the same way you can with ForceArray. (I already sent Grant a patch.) The problem is that you have to know the XML, you have to know what tags may be repeated, what tags have only optional attributes etc. And once you have to do that it's no longer that simple.

Welcome XML::Rules::inferRulesFromExample() and XML::Rules::inferRulesFromDTD(). These two allow you to build a list of rules for XML::Rules out of one or more example XMLs or a DTD and those rules will instruct the module to build a datastructure equivalent to a well set XML::Simple::XMLin. For example like this:

use XML::Rules; my $parser = XML::Rules->new( rules => XML::Rules::inferRulesFromExample( $XML), ); my $ref = $parser->parse( $XML);
And it's up to you whether you generate the ruleset each time or just once (which, if you use the inferRulesFromExample is not only quicker, but also safer). And you may print the generated ruleset and use it as the basis for a more specialised one. Changing the rules to skip some tags, to ignore those optional attributes and always keep just the content, specify a subroutine to be executed for a tag, etc.

my $XML = <<'*END*'; <root> <sub> <a>foo</a> <a>bar</a> <b>blah</b> </sub> <sub> <a>baz</a> <b x="4">blah blah</b> </sub> </root> *END* use XML::Simple; use Data::Dumper; my $ref = XMLin( $XML); print Dumper($ref); __END__
my $XML = <<'*END*'; <root> <sub> <a>foo</a> <a>bar</a> <b>blah</b> </sub> <sub> <a>baz</a> <b x="4">blah blah</b> </sub> </root> *END* use XML::Rules; use Data::Dumper; my $parser = XML::Rules->new( rules => XML::Rules::inferRulesFromExample( $XML), ); my $ref = $parser->parse( $XML); print Dumper($ref);

In reply to Simpler than XML::Simple by Jenda

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 avoiding work at the Monastery: (4)
As of 2024-03-29 09:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found