Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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'

In reply to Re: problem on xml parsing with xml::Simple module by tobyink
in thread problem on xml parsing with xml::Simple module by sarf13

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-25 06:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found