Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

hi i want to retrieve the element and values from xml document

by joyeux (Initiate)
on Nov 20, 2012 at 11:51 UTC ( #1004699=perlquestion: print w/ replies, xml ) Need Help??
joyeux has asked for the wisdom of the Perl Monks concerning the following question:

Hi..i have a xml document..i want to retrieve all the xml element and its values in perl..which way i have to do that..xpath or data dumber..which is more efficient..could plz someone guide me.. Thanks and Regards Poongodi

Comment on hi i want to retrieve the element and values from xml document
Re: hi i want to retrieve the element and values from xml document
by tobyink (Abbot) on Nov 20, 2012 at 12:55 UTC

    That's a rather open-ended question. But here's a quick demonstration of some of XML::LibXML's capabilities...

    use v5.14; use syntax qw( junction ); use XML::LibXML 2 ':all'; my $xml = XML::LibXML->load_xml( IO => \*DATA ); my ($foo) = $xml->getElementsByTagName('foo'); say $foo; # says '<foo>Hello World</foo>' say $foo->textContent; # says 'Hello World' my ($bar) = $xml->findnodes('//bar'); # xpath say $bar; # says '<bar quux="1"/>' say $bar->{quux}; # says '1' my @baz = $xml->findnodes('//baz'); say $_->{xyzzy} for @baz; # says 'a' then 'b' then 'c' my ($comment) = grep { $_->nodeType eq none(XML_ELEMENT_NODE, XML_TEXT_NODE) } $xml->documentElement->childNodes; say $comment; __DATA__ <document> <foo>Hello World</foo> <bar quux="1" /> <baz xyzzy="a" /> <baz xyzzy="b" /> <baz xyzzy="c" /> <!-- yes, this is part of the DOM --> </document>
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

      Hi tobyink, thank u so much for ur help..

      1.This is my xml document

      <Order>
      <Date>2003/07/04</Date>
      <CustomerId>123</CustomerId>
      <CustomerName>Acme Alpha</CustomerName>

      <Item>
      <ItemId> 987</ItemId>
      <ItemName>Coupler</ItemName>
      <Quantity>5</Quantity>
      </Item>
      <Item>
      <ItemId>579</ItemId>
      <ItemName>Clasp</ItemName>
      <Quantity>1</Quantity>
      </Item>
      </Order>

      2.my output should be
      Date:2003/07/04
      CustomerId:123...etc..like this

      3.i have to select the element nodes without having to
      specify their names..and retrieve the element
      data value..in the same order as the elements in xml document

      could plz help me for this..

Re: hi i want to retrieve the element and values from xml document
by choroba (Abbot) on Nov 20, 2012 at 15:44 UTC
    Using xsh, a wrapper around XML::LibXML:
    open file.xml ; for //text() { if xsh:matches(., '\S') echo concat(name(..), ':') (.) ; }
    Outputs:
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (12)
As of 2014-11-28 14:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (197 votes), past polls