Question

My problem is that my XML looks like this:

<A>
    <INIT>
        <wwn>myvalue1</wwn>
           .
           [one or more values]
           .
        <wwn>myvalue2</wwn>
    </INIT>
</A>

and it's the values of those multiple nodes that I need. There will always be 1 or more of them, but i have no way of knowing how many.

I found out about using findvalue() that hard way, and as documented in several places, the values of multiple nodes will be concatenated and returned as a single string value by findvalue().

My thought was to use findnodes, and for each node found get the value of that node. Sounds like a plan because I see the library contains

$content = $node->nodeValue;

as an option, and the documentation suggested this was just what I needed . Here's what I've tried:

for my $INIT ($A->findnodes('./INIT')) {
    foreach my $Wwn ($INIT->findnodes('./wwn')) {
        my $wwn = $Wwn->nodeValue;
    }
}

This returns an error telling me $wwn does not get initialized. I have shown that the value of $Wwn is indeed the literal node string

<wwn>myvalue1</wwn>.

So close, yet so far. For each value of the node I need to write a record, so I'd like to use the foreach{} so I can process each value one at a time. I take it I'm not actually sending 'a node' to nodeValue, but am sending something else it doesn't recognize. If $INIT is not a node, then what else can it be?

Any help greatly appreciated.

Was it helpful?

Solution

The approach to iterate over the nodes having the 'values' I needed was sound, but I lack the understanding of what a node's 'name' and 'value' mean. After determining that nodeValue() has no meaning for the node type I'm looking at, I tried textContent().

foreach my $Wwn ($A->findnodes('./INIT/wwn')) {
    my $wwn = $Wwn->textContent();
    [do some stuff];
}

This solves the problem exactly as I need it to.

So, the homework for the weekend is:

  1. What are node types?
  2. What is a 'text node' and how does it differ from others?
  3. What is a node's 'name' and 'value' and how do they relate to 'content'?
  4. What is node, really?
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top