I am trying to read tags from XML using LibXML. I can print all the tags; however, for some reason it also prints “text” tag which is not part of my XML. Anyone can explain to me what causes this behavior? Any work around? If next time I have an XML tag with a “text” tag, then how can I distinguish that “text” tag from the extra “text” tag that is generated by LibXML?
Here’s my XML file:
<?xml version="1.0"?>
<log>
<logentry
revision="935">
<author>darwin</author>
<date>2011-06-28T01:00:18.997106Z</date>
<msg>reintegrate branch</msg>
</logentry>
<logentry
revision="931">
<author>darwin</author>
<date>2011-06-21T22:15:01.881607Z</date>
<msg>reintegrate branch</msg>
</logentry>
</log>
I read the XML using the following code:
my $parser = XML::LibXML->new();
my $doc = $parser->parse_string($svnInfo);
my $root = $doc->getDocumentElement();
my @nodes=$root->childNodes();
foreach my $child(@nodes) {
my $name = $child -> nodeName();
my @atts = $child -> getAttributes();
print "\n${name} (";
print ")\n";
}
This is my output:
text ()
logentry ()
text ()
logentry ()
text ()
Thanks for the help.
Try adding
$parser->keep_blanks(0)beforemy $doc = $parser->parse_string($svnInfo);EDIT changed no_blanks to keep_blanks