Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: using xml::simple with unknown xml structure

by shiza (Hermit)
on Jul 14, 2005 at 16:15 UTC ( [id://474936]=note: print w/replies, xml ) Need Help??


in reply to using xml::simple with unknown xml structure

This code worked for me (using OO interface):
#!/usr/bin/perl use strict; use XML::Simple; use Data::Dumper; my $xml_obj = XML::Simple->new(ForceArray => ['tables']); # xml my $xml = $xml_obj->XMLin('test.xml'); print Dumper($xml); exit;
Also, double check for any errors in your xml file.

Output:
$VAR1 = { 'detail' => { 'rate' => { 'name' => 'RATE', 'type' => 'number', 'size' => '', 'mnemonic' => 'rate' }, 'date' => { 'name' => 'The_Date', 'type' => 'date', 'size' => '8', 'mnemonic' => 'date' }, 'name' => 'schema.detail', 'requiredfields' => 'date,rate,scenarioname' }, 'junk1' => { 'name' => 'rates', 'requiredfields' => 'date,rate', 'type' => 'junk1' }, 'junk2' => { 'name' => 'demands', 'requiredfields' => '', 'type' => 'junk2' }, 'xyz' => { 'cpr' => { 'required' => 'true', 'calc' => 'CASE WHEN SUM(a) != 0 THEN roun +d((1-power(1-sum(b/100*a)/sum(a),12))*100,2) ELSE SUM(0) END', 'name' => 'CPR', 'cohort' => 'false', 'type' => 'summed', 'size' => '8', 'mnemonic' => 'cpr' }, 'obsdate' => { 'required' => 'false', 'calc' => 'to_char(asofdate,\'mm/dd/yy +yy\')', 'name' => 'asofdate', 'cohort' => 'true', 'type' => 'summed', 'size' => '8', 'mnemonic' => 'obsdate' }, 'model' => { 'required' => 'true', 'calc' => 'trim(\'model\')', 'name' => 'model', 'cohort' => 'true', 'type' => 'summed', 'size' => '', 'mnemonic' => 'model' }, 'name' => 'schema.xyz', 'requiredfields' => 'obsdate,wac,wala,wam', 'loancount' => { 'required' => 'false', 'calc' => 'sum(LOANCOUNT)', 'name' => 'LoanCount', 'cohort' => 'fale', 'type' => 'summed', 'size' => '8', 'mnemonic' => 'loancount' }, 'new' => { 'required' => 'false', 'name' => 'ISNEW', 'cohort' => 'true', 'type' => 'char', 'size' => '', 'mnemonic' => 'new' } } };

Code w/ loop:
#!/usr/bin/perl use strict; use XML::Simple; use Data::Dumper; my $xml_obj = XML::Simple->new(ForceArray => ['tables']); # xml my $xml = $xml_obj->XMLin('test.xml'); for my $table (keys %$xml) { print Dumper($table); } exit;

Replies are listed 'Best First'.
Re^2: using xml::simple with unknown xml structure
by shemp (Deacon) on Jul 14, 2005 at 17:10 UTC
    What is happening is that the top level tag doesnt exist in the input structure. This is always the case with XML::Simple from what i know. So for your file, the top-level keys in the $xml hash will be the tags directly under the outer enclosing table tag.

    Just started using XML::Simple a couple weeks ago myself, it takes a bit to get used to, but becomes quite powerful once you get used to it.

      It looks like the object oriented method is getting me what I needed. I don't see a whole lot of difference with what I tried, but I am thrilled that it is working. Thanks everyone for the support.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://474936]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (4)
As of 2024-03-29 05:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found