select XML node containing a matching string

P

pal.saurabh

I've an XML file as follows.

<?xml version="1.0"?>
<packet>
<proto name="bootp" size="272" pos="46">
<field name="bootp.type" size="1" pos="46" show="1" value="01"/></
field>
<field show="Agent Circuit ID: 3031323334" size="7" pos="310"
value="01053031323334"/></field>
</proto>
<packet>

I want to select a node in this tree where node field[show] has a
string that contains string "Agent Circuit ID". How can I do it using
javascript and xmlDoc.selectSingleNode function?

I tried using //field[@show=~"Agent Circuit ID"] and //
field[contains(@show,'Agent Circuit ID')], but '=~' and
'contains' are not recognised within javascript.

thanks in advance.

Saurabh
 
M

Martin Honnen

I've an XML file as follows.

<?xml version="1.0"?>
<packet>
<proto name="bootp" size="272" pos="46">
<field name="bootp.type" size="1" pos="46" show="1" value="01"/></
field>
<field show="Agent Circuit ID: 3031323334" size="7" pos="310"
value="01053031323334"/></field>
</proto>
<packet>

That sample is not well-formed XML at all, you need to correct that to:

<?xml version="1.0"?>
<packet>
<proto name="bootp" size="272" pos="46">
<field name="bootp.type" size="1" pos="46" show="1" value="01"/>
<field show="Agent Circuit ID: 3031323334" size="7" pos="310"
value="01053031323334"/>
</proto>
I want to select a node in this tree where node field[show] has a
string that contains string "Agent Circuit ID". How can I do it using
javascript and xmlDoc.selectSingleNode function?

I tried using //field[@show=~"Agent Circuit ID"] and //
field[contains(@show,'Agent Circuit ID')], but '=~' and
'contains' are not recognised within javascript.

Make sure you set the SelectionLanguage property, then contains should
work e.g.

var xmlDocument = new ActiveXObject('Msxml2.DOMDocument.3.0');
xmlDocument.async = false;
xmlDocument.load('file.xml');

xmlDocument.setProperty('SelectionLanguage', 'XPath');

var field = xmlDocument.selectSingleNode(
'packet/proto/field[contains(@show, "Agent Circuit ID")]');

alert(field.xml);
 

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
474,008
Messages
2,570,268
Members
46,867
Latest member
Lonny Petersen

Latest Threads

Top