Search code examples
xmlperlxml-parsingxml-libxml

Find value of child node


Ok, this is a pretty rudimentary question, but I'm new to Perl and I honestly can't seem to find the answer anywhere, even though I'm sure it will be ridiculously simple.

Let's say I have the following XML schema:

<root>
    <parentNode status="Good">
        <A>
            <B>
                <value><![CDATA[This is my value]]</value>
            </B>
        </A>
    </parentNode>
</root>

Assume there are going to be multiple parentNodes with varying statuses.

I'm trying to write a script that will give me the content of each of the value nodes of the parentNodes where status isn't "Good"

Using the following code I've been able to successfully get the correct parentNodes:

my $parser = XML::LibXML->new();
my $tree = $parser->parse_file($xml_file);
my $root = $tree->getDocumentElement;
my @records = $root->findnodes("//parentNode");
foreach my $node (@records) {
    my $resultAtt = $node->getAttribute('status');
    next if $resultAtt ne "Good";

But when I try:

my $val = $node->findvalue("value");

I get nothing.

Additionally, I'm really just interested in the "This is my value" part. When you read the value, does the CDATA affect it at all?


Solution

  • Your XPath needs to be implicit.

    Rather than using :my $val = $node->findvalue("value"); you should use: $val = $node->findvalue('./A/B/value');

    You should have success :D

    Copying your code (and fixing the CDATA to have the closing angle bracket) and using the above code snippet instead:

    $ ./test2.pl 
    Found the value: This is my value
    $