Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: problem on xml parsing with xml::Simple module

by tobyink (Abbot)
on Aug 07, 2012 at 12:18 UTC ( #985959=note: print w/ replies, xml ) Need Help??


in reply to problem on xml parsing with xml::Simple module

The problem is that XML::Simple's default settings are too clever for their own good. It will sometimes represent your <query> elements as a hashref (i.e. when the element has any attributes or child elements) and sometimes treat it as a string.

Changing the XML::Simple ForceContent setting (switching it on) should solve this, but it will probably cause other issues to pop up instead.

My personal recommendation is to switch from XML::Simple to a better XML library. There is a "sweet spot" where things like XML::Simple are awesome, but in my experience it's quite a small sweet spot, and once you're outside that sweet spot it actually becomes much easier to use a more powerful XML library.

use 5.010; use strict; use warnings; use XML::LibXML 1.70; use Text::Trim 'trim'; my $xml = XML::LibXML->load_xml(location => 'C:\Users\Desktop\derived_ +tif_cd.XML'); foreach my $query ($xml->findnodes('//queries/query')) { # Join together the content within the <query> element # but filter out child elements. Just want text. my $sql = join q(), grep { $_->isa('XML::LibXML::Text') } $query->childNodes; say trim($sql); }
perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'


Comment on Re: problem on xml parsing with xml::Simple module
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://985959]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (14)
As of 2015-07-30 12:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (271 votes), past polls