Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

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'

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (15)
As of 2018-06-22 14:05 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (124 votes). Check out past polls.