Link to home
Start Free TrialLog in
Avatar of mcgilljd
mcgilljd

asked on

How do I translate XML into perl variables?

I want to collect InstName and InstID attributes from each node as perl varaiable $InstName and $InstID, then enter $InstName and $InstID into a mysql table.

I think it is something like this:
$xs0 = XML::Simple->new();
$gv8apidata = $xs0->XMLin($returnXML);
print "InstID: $gv8apidata->{'DATA'}{'INSTDEFINITION'}{'InstID'} \n";

but I get error:
encoding specified in XML declaration is incorrect at line 1, column 30, byte 30
 at C:/Perl/lib/XML/Parser.pm line 187


<?xml version="1.0" standalone='yes'?>
<DATA xmlns="data.com">
<INSTDEFINITION InstID="20002149" InstType="Sequential Instrument" InstCode="Insurance Corp" InstName="Z" UnitCode="Contracts" Unit="Contrac
ts" Live="true" EngineID="3" TermFormatID="80" CreditControlTypeID="0" PriceSort
erID="2" AliasBundleID="-2"><DERIVEDFROM InstID="20002148" InstType="Atomic Inst
rument" InstCode="Z" InstName="Z" UnitCode="Contracts" Unit="Contracts" Live
="false" EngineID="3" TermFormatID="1" CreditControlTypeID="0" PriceSorterID="-2
" AliasBundleID="-2" SeriesOfSessionsID="3"/><SEQUENCE SeqID="1000001" SeqName="
Monthly"/></INSTDEFINITION>
<INSTDEFINITION InstID="20001023" InstType="Atomic Instrument" InstCode="ZT" I
nstName="ZT" UnitCode="Contracts" Unit="Contracts" Live="false" EngineID="3" T
ermFormatID="1" CreditControlTypeID="0" PriceSorterID="-2" AliasBundleID="-2" Se
riesOfSessionsID="3"/>
<INSTDEFINITION InstID="20001024" InstType="Sequential Instrument" InstCode="Companies, Inc" InstName="ZT" UnitCode="Contracts" Unit="Contracts" Live
="true" EngineID="3" TermFormatID="80" CreditControlTypeID="0" PriceSorterID="2"
 AliasBundleID="-2"><DERIVEDFROM InstID="20001023" InstType="Atomic Instrument"
InstCode="ZT" InstName="ZT" UnitCode="Contracts" Unit="Contracts" Live="fals
e" EngineID="3" TermFormatID="1" CreditControlTypeID="0" PriceSorterID="-2" Alia
sBundleID="-2" SeriesOfSessionsID="3"/><SEQUENCE SeqID="1000001" SeqName="Monthl
y"/></INSTDEFINITION></DATA>

Open in new window

Avatar of Adam314
Adam314

Attach the XML file.  What you posted doesn't give any problems.
ASKER CERTIFIED SOLUTION
Avatar of Adam314
Adam314

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mcgilljd

ASKER

what does $xml represent in print "InstID=$xml->{INSTDEFINITION}[0]{InstID}\n";?

I changed:

$xs0 = XML::Simple->new();
$gv8apidata = $xs0->XMLin($returnXML);
print "InstID: $gv8apidata->{'DATA'}{'INSTDEFINITION'}{'InstID'} \n";

to this:

print "Highest index=$#{$returnXML->{INSTDEFINITION}}\n";


I tried using your code and get
Highest index=-1

does that make any sense?

I cant send the xml it is large.
I used $xml instead of $gv8apidata.  Just change the $xml in what I posted to $gv8apidata, and it'll work.
still get:
 encoding specified in XML declaration is incorrect at line 1, column 30, byte 30
 at C:/Perl/lib/XML/Parser.pm line 187
Added
$returnXML =~ s/UTF-16/UTF-8/;
because xml::simple only handles utf-8


Now I get:

not well-formed (invalid token) at line 313, column 83, byte 130100 at C:/Perl/l
ib/XML/Parser.pm line 187
Did you get it solved?  You accepted an answer.  Post again if there is still a problem.