Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
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

Replies are listed 'Best First'.
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 (Chancellor) 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 lurking in the Monastery: (5)
As of 2016-10-01 22:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How many different varieties (color, size, etc) of socks do you have in your sock drawer?






    Results (9 votes). Check out past polls.