Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

care to illustrate use of xml cpan modules?

by BigBear (Initiate)
on Jul 09, 2012 at 21:16 UTC ( [id://980764]=perlquestion: print w/replies, xml ) Need Help??

BigBear has asked for the wisdom of the Perl Monks concerning the following question:

how do I access xml attribute details using XML::Simple and/or XML::Mini::Document or XML::LibXML ?

I am trying to understand and compare 4 ways in which I could use perl cpan xml modules to extract some info from a bunch of given XML files.

I have written some proof of concept code using XML::XPath and am left wondering how this "simple" task would be solved by using on of the other XML modules? I am a beginner in some ways and learn best by observing a problem and then copying that sample and modyfying it to my needs.

my perl XML::XPath code #!/usr/bin/perl #file: show-barcodes-with-XML-XPath.pl #!/usr/bin/perl use XML::XPath; my $file = "00000002.TIF.xml"; my $xp = XML::XPath->new(filename => $file); my $barcode_code39 = $xp->find('/barcodes/source/index/symbol[@type="C +ODE-39"]/data'); my $barcode_qrcode = $xp->find('/barcodes/source/index/symbol[@type="Q +R-Code"]/data'); $barcode_code39 = substr($barcode_code39, 0, 10); print ("Code 39: ", $barcode_code39, "\n"); print ("QR-Code: ", $barcode_qrcode, "\n"); #print ("processed: " . $file . "\n"); #file: 00000002.TIF.xml <barcodes xmlns='http://zbar.sourceforge.net/2008/barcode'> <source href='00000002.TIF'> <index num='0'> <symbol type='QR-Code' quality='1'><data><![CDATA[0037000249]]></data> +</symbol> <symbol type='CODE-39' quality='102'><data><![CDATA[0037000249P]]></da +ta></symbol> </index> </source> </barcodes>

How would I employ XML::Simple to do the "same" thing ?

And how would I write the same thing using XML::Mini::Document ?

And lastly how would I write the same thing using XML::LibXML ?

Thanks for any insights into how this would be done using one of the above

Replies are listed 'Best First'.
Re: care to illustrate use of xml cpan modules?
by tobyink (Canon) on Jul 09, 2012 at 22:59 UTC

    With XML::LibXML it's pretty similar. The biggest change is that libxml sticks rigidly to the XPath spec and forces you to use a namespace prefix for matching element names in the 'http://zbar.sourceforge.net/2008/barcode' namespace.

    use XML::LibXML; my $file = '00000002.TIF.xml'; my $xp = XML::LibXML::XPathContext->new( XML::LibXML->load_xml(location => $file), ); $xp->registerNs(b => 'http://zbar.sourceforge.net/2008/barcode'); my $barcode_code39 = $xp->find('/b:barcodes/b:source/b:index/b:symbol[ +@type="CODE-39"]/b:data'); my $barcode_qrcode = $xp->find('/b:barcodes/b:source/b:index/b:symbol[ +@type="QR-Code"]/b:data'); $barcode_code39 = substr($barcode_code39, 0, 10); print ("Code 39: ", $barcode_code39, "\n"); print ("QR-Code: ", $barcode_qrcode, "\n");
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: care to illustrate use of xml cpan modules?
by mirod (Canon) on Jul 10, 2012 at 04:45 UTC

    If you want comparisons of various XML modules, have a look at the Ways to Rome series. It presents the same task, done with many different modules.

Re: care to illustrate use of xml cpan modules?
by Mr. Muskrat (Canon) on Jul 10, 2012 at 18:37 UTC

    This is how I would do it with XML::Simple.

    #!/usr/bin/perl use strict; use warnings; use XML::Simple; my $file = '00000002.TIF.xml'; my $xml = XMLin( $file, KeyAttr => { symbol => 'type' }, ); my %barcode = ( code39 => substr( $xml->{source}{index}{symbol}{'CODE-39'}{data}, +0, 10 ), qrcode => $xml->{source}{index}{symbol}{'QR-Code'}{data}, ); print "Code 39: $barcode{code39}\n"; print "QR_Code: $barcode{qrcode}\n";
Re: care to illustrate use of xml cpan modules?
by grantm (Parson) on Jul 11, 2012 at 03:12 UTC

    This node compares examples of using XML::Simple and XML::LibXML. I strongly recommend the latter and definitely don't recommend XML::XPath.

Re: care to illustrate use of xml cpan modules?
by Anonymous Monk on Jul 09, 2012 at 21:19 UTC
    Well, each one of the candidates comes with lots of examples, look in the MANIFEST

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://980764]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (7)
As of 2024-04-23 12:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found