Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

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

by tobyink (Abbot)
on Nov 20, 2012 at 12:55 UTC ( #1004705=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1004705]
help
Chatterbox?
[chacham]: I'm sure this is old, but i just came across it: If you put a million monkeys at a million keyboards, one of them will eventually write a Java program. The rest of them will write Perl programs.
[LanX]: lol
[Corion]: Woo hoo! A new Auralnauts episode!
LanX mean unfair and funny
[LanX]: but most java code looks like from one million monkey using eclipse. ..

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (13)
As of 2018-04-23 16:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?