Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Get the XML nodes.

by Jjayant (Initiate)
on Jul 24, 2013 at 11:29 UTC ( [id://1046081]=perlquestion: print w/replies, xml ) Need Help??

Jjayant has asked for the wisdom of the Perl Monks concerning the following question:

I want to parse an XML file and collect the required nodes for further processing. Following is my code snippet. But I am getting error for the same as "Not a HASH reference".
use XML::Simple; use Data::Dumper; $xml = new XML::Simple (KeyAttr => [],suppressempty => 1,ForceArray => + ['pointTmc']); $data = $xml->XMLin("try.xml"); #print Dumper($data); for my $pointTmc ( @{$data->{linears}->{linear}->{pointTmc}}) { print "$pointTmc->{dataTypes}->{dataType}->{speedSources}->{sp +eedSource}->{speed} \n"; # print "$pointTmc->{dataTypes}->{dataType}->{speedSources}->{sp +eedSource}->{timestamp} \n"; }
Can someone help me out in this.

Replies are listed 'Best First'.
Re: Get the XML nodes.
by choroba (Cardinal) on Jul 24, 2013 at 13:01 UTC
    Using XML::XSH2, a wrapper around XML::LibXML:
    open file.xml ; for /trafficmodel/linears/linear/pointTmc/dataTypes/dataType/speedSour +ces/speedSource echo @speed ;
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Get the XML nodes.
by poj (Abbot) on Jul 24, 2013 at 12:29 UTC
    for my $linear ( @{$data->{linears}->{linear}} ){ for my $pointTmc ( @{$linear->{pointTmc}} ){ print "$pointTmc->{dataTypes}->{dataType}->{speedSources}->{speedS +ource}->{speed} \n"; print "$pointTmc->{dataTypes}->{dataType}->{speedSources}->{speedS +ource}->{timestamp} \n"; } }

    poj
Re: Get the XML nodes.
by daxim (Curate) on Jul 24, 2013 at 11:35 UTC
    Provide the input XML file.
      cat try.xml <trafficmodel id="1" timestamp="07/23/2013 12:20:55 GMT" table="219"> <linears id="2"> <linear linearTmcId="219+08065"> <pointTmc pointTmcId="219+12022"> <pointTmcLength>0.0</pointTmcLength> <startDistanceOnLinear>0.0</startDistanceOnLinear> <endDistanceOnLinear>0.0</endDistanceOnLinear> <freeflowSpeed>18.6</freeflowSpeed> <uncappedAverageSpeed>6.84</uncappedAverageSpeed> <cappedAverageSpeed>6.84</cappedAverageSpeed> <travelTimeConfidence>0.7</travelTimeConfidence> <delayWithOnRampsMin>0.0</delayWithOnRampsMin> <delayWithOffRampsMin>0.0</delayWithOffRampsMin> <status>OK</status> <dataTypes> <dataType name="Historical" confidence="0.7" ratio="1.0" spe +ed="6.84"> <speedSources> <speedSource timestamp="07/23/2013 12:20:55 GMT" speed=" +6.84" sourceId="TrafficPatterns" dataId="219+12022"> <location> <linearId>219+08065</linearId> <startDistance>0.0</startDistance> <endDistance>0.0</endDistance> <duration>0</duration> </location> </speedSource> </speedSources> </dataType> </dataTypes> </pointTmc> <pointTmc pointTmcId="219P20065"> <pointTmcLength>0.10452</pointTmcLength> <startDistanceOnLinear>0.13782</startDistanceOnLinear> <endDistanceOnLinear>0.24234</endDistanceOnLinear> <freeflowSpeed>32.3</freeflowSpeed> <uncappedAverageSpeed>23.61</uncappedAverageSpeed> <cappedAverageSpeed>23.61</cappedAverageSpeed> <travelTimeConfidence>0.7</travelTimeConfidence> <delayWithOnRampsMin>0.0</delayWithOnRampsMin> <delayWithOffRampsMin>0.0</delayWithOffRampsMin> <status>OK</status> <dataTypes> <dataType name="Historical" confidence="0.7" ratio="1.0" spe +ed="23.61"> <speedSources> <speedSource timestamp="07/23/2013 12:20:55 GMT" speed=" +23.61" sourceId="TrafficPatterns" dataId="219P20065"> <location> <linearId>219+08065</linearId> <startDistance>0.13782</startDistance> <endDistance>0.24234</endDistance> <duration>0</duration> </location> </speedSource> </speedSources> </dataType> </dataTypes> </pointTmc> </linear> <linear linearTmcId="219+08343"> <pointTmc pointTmcId="219+19582"> <pointTmcLength>0.0</pointTmcLength> <startDistanceOnLinear>0.0</startDistanceOnLinear> <endDistanceOnLinear>0.0</endDistanceOnLinear> <freeflowSpeed>34.8</freeflowSpeed> <uncappedAverageSpeed>29.83</uncappedAverageSpeed> <cappedAverageSpeed>29.83</cappedAverageSpeed> <travelTimeConfidence>0.7</travelTimeConfidence> <delayWithOnRampsMin>0.0</delayWithOnRampsMin> <delayWithOffRampsMin>0.0</delayWithOffRampsMin> <status>OK</status> <dataTypes> <dataType name="Historical" confidence="0.7" ratio="1.0" spe +ed="29.83"> <speedSources> <speedSource timestamp="07/23/2013 12:20:55 GMT" speed=" +29.83" sourceId="TrafficPatterns" dataId="219+19582"> <location> <linearId>219+08343</linearId> <startDistance>0.0</startDistance> <endDistance>0.0</endDistance> <duration>0</duration> </location> </speedSource> </speedSources> </dataType> </dataTypes> </pointTmc> </linear> </linears> </trafficmodel>

        use Data::Dumper; to see what data structure XML::Simple produced or even better dump XML::Simple and use XML::Rules instead.

        Jenda
        Enoch was right!
        Enjoy the last years of Rome.

Re: Get the XML nodes.
by Anonymous Monk on Jul 24, 2013 at 11:57 UTC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2024-03-29 01:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found