search inside xml with javascript (FF and IE)

Q

quindo

Hello,

I've searched the internet but couldn't find an answer so I'm hoping
someone here can help me.

A webpage recieves XML from the server using xmlhttp.
What I want to do is search in this xml browser-side.

XML-example:

<company>
<equipment>
<trackno>1</trackno>
<icode>ruthd</icode>
</equipment>
<equipment>
<trackno>4</trackno>
<icode>rdke</icode>
</equipment>
</company>

I want to display the icode from equipment with trackno 4.

Serverside I would have done that using XPath :
SelectSingleNode("/company/equipment [trackno='4']/icode")

How do I do this in Browserside Javascript.
The code has to work in both FireFox and IE.

Greetings,
Pieter
 
M

Martin Honnen

Serverside I would have done that using XPath :
SelectSingleNode("/company/equipment [trackno='4']/icode")

How do I do this in Browserside Javascript.
The code has to work in both FireFox and IE.

Firefox supports XPath using the W3C DOM Level 3 XPath API so you would
use e.g.
var icode = httpRequest.responseXML.evaluate(
"/company/equipment[trackno='4']/icode",
httpRequest.responseXML,
null,
XPathResult.FIRST_ORDERED_NODE_TYPE,
null
).singleNodeValue;
if (icode != null) {
// now access e.g. icode.textContent
}

IE 6 and later use MSXML 3 to have XPath support so there you would do e.g.
var xmlDoc = httpRequest.responseXML;
xmlDoc.setProperty('SelectionLanguage', 'XPath');
var icode =
xmlDoc.selectSingleNode("/company/equipment[trackno='4']/icode");
if (icode != null) {
// now access e.g. icode.text
}
 
Q

Quindo

Serverside I would have done that using XPath :
SelectSingleNode("/company/equipment [trackno='4']/icode")
How do I do this in Browserside Javascript.
The code has to work in both FireFox and IE.Firefox supports XPath using the W3C DOM Level 3 XPath API so you would
use e.g.
var icode = httpRequest.responseXML.evaluate(
"/company/equipment[trackno='4']/icode",
httpRequest.responseXML,
null,
XPathResult.FIRST_ORDERED_NODE_TYPE,
null
).singleNodeValue;
if (icode != null) {
// now access e.g. icode.textContent
}

IE 6 and later use MSXML 3 to have XPath support so there you would do e.g.
var xmlDoc = httpRequest.responseXML;
xmlDoc.setProperty('SelectionLanguage', 'XPath');
var icode =
xmlDoc.selectSingleNode("/company/equipment[trackno='4']/icode");
if (icode != null) {
// now access e.g. icode.text
}

This looks like something I could use.
Thank You!

Pieter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top