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)?
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.