select XML node containing a matching string

Discussion in 'XML' started by pal.saurabh@gmail.com, May 16, 2007.

  1. Guest

    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
     
    , May 16, 2007
    #1
    1. Advertising

  2. wrote:
    > 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>
    </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.


    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);

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, May 16, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    1,645
  2. CB
    Replies:
    1
    Views:
    613
    Johannes Koch
    Oct 2, 2003
  3. bearclaws
    Replies:
    3
    Views:
    601
    bearclaws
    Mar 10, 2005
  4. Tjerk Wolterink
    Replies:
    2
    Views:
    1,505
    Dimitre Novatchev
    Aug 24, 2006
  5. clover2411
    Replies:
    4
    Views:
    983
    Andy Dingley
    Jan 23, 2007
Loading...

Share This Page