Solved

How do I translate XML into perl variables?

Posted on 2008-10-02
8
861 Views
Last Modified: 2012-05-05
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

0
Comment
Question by:mcgilljd
  • 4
  • 4
8 Comments
 
LVL 39

Expert Comment

by:Adam314
ID: 22627401
Attach the XML file.  What you posted doesn't give any problems.
0
 
LVL 39

Accepted Solution

by:
Adam314 earned 500 total points
ID: 22627437
Note that you would get the parameters you are looking for like:
    print "InstID=$xml->{INSTDEFINITION}[0]{InstID}\n";
Where the 0 is replaced with the index of which INSTDEFINITION for which you want the data, 0 through 2 in the XML you posted.  You can get the highest index with this:
    print "Highest index=$#{$xml->{INSTDEFINITION}}\n";
0
 

Author Comment

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

0
 

Author Comment

by:mcgilljd
ID: 22627612
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.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 39

Expert Comment

by:Adam314
ID: 22627794
I used $xml instead of $gv8apidata.  Just change the $xml in what I posted to $gv8apidata, and it'll work.
0
 

Author Comment

by:mcgilljd
ID: 22627920
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
0
 

Author Comment

by:mcgilljd
ID: 22628124
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
0
 
LVL 39

Expert Comment

by:Adam314
ID: 22629048
Did you get it solved?  You accepted an answer.  Post again if there is still a problem.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
perl script help 5 229
perl match and sort unique result 2 122
hard perl script 16 146
Add additional column to .csv using Perl. 8 120
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now