Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

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 (Bishop) 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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1023057]
Approved by tobyink
help
Chatterbox?
[1nickt]: choroba Understood. I'm wondering about the logic in a cpanfile. If the perl doesn't support threads, it's easy: require MCE. If the perl does support threads, as you say the user has a choice, so require both? Or, assume that irrespective of the choide
[1nickt]: ... choice, the threaded-perl user *probably* wants to use threads, therefore only require threads and leave MCE out, allowing the user to manually install if desired?
[choroba]: just require any of MCE and threads. Not sure if that's possible in the cpanfile, but should be possible early in the Makefile
[1nickt]: The app expects threads as default anyway, right?
[choroba]: that's true
[1nickt]: expects threads *to be* the default
[1nickt]: I think cpanfile can handle the logic. testing now.

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (14)
As of 2017-10-18 13:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    My fridge is mostly full of:

















    Results (244 votes). Check out past polls.

    Notices?