Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

finding each and every node of a xml document

by ritvik_raj2012 (Initiate)
on Apr 23, 2012 at 06:54 UTC ( #966526=perlquestion: print w/ replies, xml ) Need Help??
ritvik_raj2012 has asked for the wisdom of the Perl Monks concerning the following question:

hi monks,
please help me out in finding a solution. i want to list out each and every node of a xml document.
if you guys can tell me this solution through xml::libxml that would be nice.

Comment on finding each and every node of a xml document
Re: finding each and every node of a xml document
by Anonymous Monk on Apr 23, 2012 at 07:13 UTC

    hi monks, please help me out in finding a solution. i want to list out each and every node of a xml document. if you guys can tell me this solution through xml::libxml that would be nice.

    I'm pretty sure you already got exactly that, what is the problem?

Re: finding each and every node of a xml document
by choroba (Abbot) on Apr 23, 2012 at 08:21 UTC
    Using XML::XSH2, which is a wrapper around XML::LibXML:
    for (//* | //@* | //comment() | //text() | //processing-instruction()) + ls . ;
Re: finding each and every node of a xml document
by tobyink (Abbot) on Apr 23, 2012 at 09:36 UTC

    This example should do the trick. Note that DTD nodes don't appear in the XPath model, so I grep for those first.

    use 5.010; use XML::LibXML 1.70 ':all'; my $dom = XML::LibXML-> load_xml(IO => \*DATA); my @nodes = ( (grep { $_->nodeType == XML_DTD_NODE } $dom->childNodes), $dom->findnodes('//node() | //@*'), ); foreach (@nodes) { say "Node name: ", $_->nodeName; say "Node path: ", $_->nodePath; say "---"; } __DATA__ <!DOCTYPE foo> <foo> <?processing-instruction?> <!-- a comment --> <bar attr="1" /> </foo>
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2014-08-28 05:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (257 votes), past polls