1

I am parsing XML with Javascript. My XML looks like this:

<?ml version="1.0" encoding="UTF-8">
<IPC_XML>
<dfr>
<dfr>
<data>
<analog>
    <channel index="0">
        <label>Channel 1</label>
    </channel>
    <channel index="1">
        <label>Channel 2</label>
    </channel>
</analog>
</data>
</dfr>
</dfr>
</IPC_XML>

Ignore the strange structure (out of my control). I am running this JS code:

function updateAnalogCfgValues (xmlDoc) {
var analogCfgs = xmlDoc.selectNodes ("//channel");
var cfg = analogCfgs.nextNode ();
var cnt = 1;

while (cfg !== null) {
    var node = document.getElementById ("aChan"+cnt);
    var tmp = document.createElement ("b");
    tmp.innerText = cfg.selectSingleNode ("//label").text;
    node.appendChild (tmp);

    cfg = analogCfgs.nextNode (); cnt++;
}
}

When I look at stuff in the debugger, things seem fine. I can see the cfg variable changing and I can see that its first child is the <label> element whose value is also changing. The problem is, when I look at the HTML, all I see is 'Channel 1' repeated over and over. That text never changes. Any ideas?

EDIT: Any chance my selectSingleNode call is now 'refreshing' (for lack of a better term)?

2
  • First thing I'd try is to change //label to /label - but I can't test so I'm not sure if its an answer or not... Commented May 31, 2011 at 16:42
  • With '/label', I get an exception for a null object not having a text property. Commented May 31, 2011 at 16:47

1 Answer 1

1

Looking at an XPath tutorial and getting some help from a co-worker led to the answer. I needed my search string to be .//Label. This selects my current channel node and then looks for the label node. Before, it was just searching the whole document for the first label node, which is why things didn't change.

Sign up to request clarification or add additional context in comments.

1 Comment

That was my initial guess (that it was going to the root) - but Iassumed that "cfg.selectSingleNode" would implicitly include the ".". That's ever so slightly odd...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.