Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
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
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?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (8)
As of 2014-08-22 00:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (144 votes), past polls