martini-bonanza
martini-bonanza

Reputation: 1591

Perl XML::DOM parser usage

I'm trying to get the numbers of errors in the fifth line of this XML file:

<summary>
  <result usecase="CRUD" target="AppHost">
    <testcase size="1" cvus="1">
      <sequence tps="0.25" totaltx="1" name="CRUD" min="3515" max="3515" errors="0" average="3515.0">
        <node tps="0.25" totaltx="1" name="localhost" min="3515" max="3515" errors="0" average="3515.0">
            […]
        </node>
      </sequence>
    </testcase>
  </result>
</summary>

Using XML::DOM::Parser and this code:

my $parser = new XML::DOM::Parser;
my $doc = $parser->parsefile($file);
my $root = $doc->getDocumentElement();

foreach my $child ($root->getChildNodes) {
    print $child->getNodeName();
    print "\n";
}

But instead of only printing "result", I get this:

#text
result
#text

To reach the parameters of the "node" node in the fifth line, I want to use the getFirstChild method, but I can't because it looks for children of "#text".

What is this #text object? What should I do to reach the fifth node?

Thank you,

Kevin

Upvotes: 2

Views: 5335

Answers (1)

Matt Healy
Matt Healy

Reputation: 18531

Try:

#!/usr/bin/perl

use XML::DOM;
use XML::Parser;

$file = "test.xml";

my $parser = new XML::DOM::Parser;
my $doc = $parser->parsefile($file);

print $doc->getElementsByTagName('node')->item(0)->getAttributeNode('errors')->getFirstChild->getNodeValue;
print "\n";

Upvotes: 4

Related Questions