Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

problem on xml parsing with xml::Simple module

by sarf13 (Beadle)
on Aug 07, 2012 at 11:21 UTC ( #985941=perlquestion: print w/ replies, xml ) Need Help??
sarf13 has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks I am using XML::Simple module for parsing my attached xml data. I wrote mentioned code for fetching query data value. It is working fine. But it is giving this error “Can't use string ("GENERATE EXPRESS STATIST") as a HASH ref while "strict refs" in use” .i think this error because of “foreach my $sql ($query->{content}) {” loop on line no 42. Can you suggest some way solve this??

Thank you very much

PERL Script

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; use SQLValidation; use XML::Simple; my $xml_obj1 = XML::Simple->new(); #my $file_path = 'C:\Users\Desktop\sql_test.xml' ; my $file_path = 'C:\Users\Desktop\derived_tif_cd.XML' ; my $xmlfile = $xml_obj1->XMLin($file_path) or die "Failed for $f +ile_path: $!"; #print Dumper $xmlfile; =c my $val = $xmlfile->{queries}->{query}->[2]->{content}; print "$val"; =cut foreach my $queries ($xmlfile->{queries}){ if($xmlfile->{queries} eq 'query') { foreach my $query (@{$queries->{query}}) { foreach my $sql ($query->{content}) { print "$sql\n"; } } } else { foreach my $query (@{$queries->{query}}) { print "---->$query\n"; } } }

XML Data File


<Common_Checks> <comments> This xml includes validations for order event types (C +hecks for Others). </comments> <queries> <comments> ORDER_TYPE_CODE and expiration date for TIF code derivati +on: </comments> <query> INSERT INTO table_name (column1, column2, colum +n3,...) select (value1, value2, value3,...); <orderTypes>order_nw_stgng</orderTypes> <orderTypes>order_or_stgng</orderTypes> <orderTypes>order_oe_stgng</orderTypes> <orderTypes>order_cr_stgng</orderTypes> <queryParam>BATCH_ID</queryParam> </query> <query> GENERATE EXPRESS STATISTICS ON drvd_tif_order_stgng +; </query> <comments> ORDER_TYPE_CODE and expiration date for TIF code derivati +on: </comments> <query> INSERT INTO table_name (column1, column2, column3,...) s +elect (value1, value2, value3,...); <orderTypes>order_nw_stgng</orderTypes> <orderTypes>order_or_stgng</orderTypes> <orderTypes>order_oe_stgng</orderTypes> <orderTypes>order_cr_stgng</orderTypes> <queryParam>BATCH_ID</queryParam> </query> <query> GENERATE EXPRESS STATISTICS ON drvd_tif_order_stgng; </query> </queries> </Common_Checks>

Comment on problem on xml parsing with xml::Simple module
Select or Download Code
Re: problem on xml parsing with xml::Simple module
by tobyink (Abbot) on Aug 07, 2012 at 12:18 UTC

    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?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (12)
As of 2014-11-27 10:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (183 votes), past polls