Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

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
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2017-08-22 23:14 GMT
Find Nodes?
    Voting Booth?
    Who is your favorite scientist and why?

    Results (344 votes). Check out past polls.