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);

In reply to Re: XML Help.. by Corion
in thread XML Help.. by FreeRollen

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.