Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Comment on

( #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

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 the shadows deepen...

    How do I use this? | Other CB clients
    Other Users?
    Others chanting in the Monastery: (5)
    As of 2018-02-22 02:00 GMT
    Find Nodes?
      Voting Booth?
      When it is dark outside I am happiest to see ...

      Results (288 votes). Check out past polls.