Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

How to Parse XML output?

by powermonk (Initiate)
on Mar 12, 2013 at 21:20 UTC ( #1023057=perlquestion: print w/replies, xml ) Need Help??
powermonk has asked for the wisdom of the Perl Monks concerning the following question:

I want to parse the below output from a webservices program to extract the value of "ref_num" i.e '2215' . How to do this ?
<?xml version="1.0" encoding="UTF-8"?> <UDSObject> <Handle>cr:401149</Handle> <Attributes> <Attribute DataType="2002"> <AttrName>ref_num</AttrName> <AttrValue>2215</AttrValue> </Attribute> <Attribute DataType="2002"> <AttrName>persistent_id</AttrName> <AttrValue>cr:401149</AttrValue> </Attribute> </Attributes> </UDSObject>

Replies are listed 'Best First'.
Re: How to Parse XML output?
by choroba (Chancellor) on Mar 12, 2013 at 21:28 UTC
    Hello powermonk, welocme to the Monastery.

    Please, use <code> ... </code> tags not only for code samples, but for data as well.

    There are several XML parsing modules on CPAN. I would recommend XML::LibXML. You can easily navigate in the XML document using XPath expressions:

    #!/usr/bin/perl use warnings; use strict; use XML::LibXML; my $xml = XML::LibXML->load_xml(location => '1.xml'); my $v = $xml->findnodes('//Attribute[AttrName="ref_num"]/AttrValue') +; print "$v\n";
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
      Thanks for the quick response. can we pass a variable name in load_xml, since the o/p I get from a webservices call is stored in a variable whose contents are xml. Thank you.
        Have you read the documentation I linked to? The details are here: XML::LibXML::Parser - DOM Parser.
        لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

        can we pass a variable name in load_xml,

        No, programs are forbidden from using variables

        Sincerely yours, the great sarcasmo

Re: How to Parse XML output?
by rjt (Deacon) on Mar 12, 2013 at 23:37 UTC

    There are many CPAN modules which can help you. For your first foray into simple XML parsing, I'd recommend XML::Simple. Despite being "simple", it still tends to do what I need most of the time.

    use 5.014; use XML::Simple; my $xmls = XMLin($xml_string); say $_->{AttrValue} for grep { $_->{AttrName} eq 'ref_num' } @{$xmls->{Attributes}->{Attribute}};

    $xmls is just an ordinary HASH ref; it's usually instructive to print it out with Data::Dump, Data::Dumper or similar:

    use Data::Dump; dd $xmls;
      I tired it, but its not working. Below is what I am doing. my $xml_string= $result->result; //Store return value of webservices function. my $tkt= $_->{AttrValue} for grep { $_->{AttrName} eq 'ref_num' } @{$xmls->{Attributes}->{Attribute}}; print $tkt Not able to capture value.
        Finally Got it. Was able to extract it using XML::LibXML by passing the string. Thanks all for your valuable help.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1023057]
Approved by tobyink
[Corion]: LanX: Ah, yeah - Frankfurt is in the quake region, but at the very border. I think I've never noticed a quake in Frankfurt myself
[marto]: good morning all
[Corion]: On another topic, in the process of Rubber Duck SoPWing, I wrote a post about the best API for generating HTTP requests (not sending them) and while writing it and fleshing out my sample implementation, I came up with some improvements to my ...
[Corion]: ... existing prototype. Cartesian Products will be proud of that module once it gets out ;)
[Corion]: Hi marto ;)

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (9)
As of 2017-01-18 10:16 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (161 votes). Check out past polls.