FreeRollen has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to parse a XML which has been giving me problems for the last day or two... However, when i run the script i unable to get results. The

children->hasChildNodes() always return 1 even if there are not child nodes?

children->textContent($name) returns the text of all the nodes including the subnodes...

$children->attributes() does not return the attribute pussycat...

use XML::LibXML; use Data::Dumper qw(Dumper); my $parser = XML::LibXML->new; my $doc = $parser->parse_file('pets.xml') or die; my $root = $doc->documentElement(); my $value = $root->nodeName; my $name; my $text; my $boolean; my $value; my $node; my $attribute; my @att; my $child; sub buildtree { my $node = shift; foreach my $children ( $node->getChildnodes ) { if( $children->nodeType eq &XML_ELEMENT_NODE ) { $name = $children->nodeName; $node = $children->nodeType; $text = $children->textContent($name); @att = $children->attributes(); $child = $children->hasChildNodes(); print "Name:$name\n"; print "Text:$text\n"; print "attrib:$attribute\n"; print "Attribute:@att\n"; print "Has Child:$child\n"; } buildtree( $children ); } } buildtree($root); <?xml version='1.0'?> <pets> <cat attribute="pussycat"> <name>Madness</name> <price>150</price> </cat> <dog> <name>Maggie</name> <owner>Rosie</owner> </dog> </pets>

Replies are listed 'Best First'.
Re: XML Help..
by Corion (Patriarch) on Jul 12, 2010 at 20:22 UTC

    I can answer at least your first question. A text() node is also a child node.

    If you want to search for certain nodes, I really recommend using XPath queries.

    The following code works for me and also finds the attributes. You didn't say how your code didn't find the attribute.

    #!perl -w use strict; use XML::LibXML; use Data::Dumper qw(Dumper); my $parser = XML::LibXML->new; my $doc = $parser->load_xml(string => <<'XML') or die; <?xml version='1.0'?> <pets> <cat attribute="pussycat"> <name>Madness</name> <price>150</price> </cat> <dog> <name>Maggie</name> <owner>Rosie</owner> </dog> </pets> XML my $root = $doc->documentElement(); my $value = $root->nodeName; sub buildtree { my $node = shift; if (! ref $node) { print "Not a reference: '$node'\n"; return }; foreach my $child ( $node->getChildnodes ) { if( $child->nodeType eq &XML_ELEMENT_NODE ) { my $name; my $text; my $boolean; my $value; my $node; my @att; my $name = $child->nodeName; my $node = $child->nodeType; my $text = $child->textContent($name); my @att = map { sprintf "%s -> %s", $_->name, $_->value } + $child->attributes(); my $has_children = $child->hasChildNodes(); print "Name:$name\n"; print "Text:$text\n"; print "Attributes:@att\n"; print "Has Child:$child\n"; } buildtree( $child ); } } buildtree($root);

      <quote>I can answer at least your first question. A text() node is also a child node.</quote> Which makes the method prettymuch useless.

      Jenda
      Enoch was right!
      Enjoy the last years of Rome.