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

Re: XML::Simple - Handling inconsistency

by Anonymous Monk
on Jan 09, 2013 at 09:00 UTC ( #1012407=note: print w/replies, xml ) Need Help??

in reply to XML::Simple - Handling inconsistency

Is there any way to make it give the similar structure?

use XML::Rules , just give xml2XMLRules the most complex detailed xml file you have, and you'll get rules/data like these

#!/usr/bin/perl -- use strict; use warnings; use Data::Dump; use XML::Rules; my $ta = XML::Rules->new( qw/ stripspaces 8 /, rules => { 'literal' => 'as is', 'binding' => 'as array no content', 'uri' => 'content', 'results' => 'no content' } ); my @xml = ( q{<results> <binding name="image_url"> <uri>Football.png</uri> </binding> <binding name="description"> <literal xml:lang="en">Second article.</literal> </binding> </results> }, q{<results> <binding name="description"> <literal xml:lang="en">First article. </literal> </binding> </results> } ); dd( my$ref = $ta->parsefile( \$_ )) for @xml; __END__ { results => { binding => [ { name => "image_url", uri => "Football.png" }, { literal => { "_content" => "Second article.", "xml:lang" => "e +n" }, name => "description", }, ], }, } { results => { binding => [ { literal => { "_content" => "First article.", "xml:lang" => "en +" }, name => "description", }, ], }, }

Sure, you could get there with XML::Simple, but I don't bother anymore

Data::Diver for navigation :)

Replies are listed 'Best First'.
Re^2: XML::Simple - Handling inconsistency
by Jenda (Abbot) on Jan 09, 2013 at 10:32 UTC

    It's possible and advised to give xml2XMLRules not one but several xml files. That way you are more likely to catch all optional tag attributes and repeatable tags. If you have a DTD it's better to use dtd2XMLRules.

    <note>xml2XMLRules and dtd2XMLRules are scripts installed with the XML::Rules module. All they do is that they call the XML::Rules::inferRulesFromExample() and XML::Rules::inferRulesFromDTD() subroutines and print the inferred rules. The inferred rules instruct XML::Rules to build a minimal consistent data structure out of your XML (only tags that may be repeated are turned to arrays, only tags that may have attributes are turned to hashes etc.).</note>

    There's a short writeup on this in Simpler than XML::Simple.

    Enoch was right!
    Enjoy the last years of Rome.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1012407]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (10)
As of 2017-02-22 00:27 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (322 votes). Check out past polls.