http://www.perlmonks.org?node_id=1004705


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

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'
  • Comment on Re: hi i want to retrieve the element and values from xml document
  • Download Code

Replies are listed 'Best First'.
Re^2: hi i want to retrieve the element and values from xml document
by joyeux (Initiate) on Nov 20, 2012 at 13:16 UTC

    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..