http://www.perlmonks.org?node_id=985959


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'