Process a string like XML

Discussion in 'Javascript' started by Johannes, Jul 21, 2008.

  1. Johannes

    Johannes Guest

    Hello,

    is there a way to process well-formed XML from a variable (string) in
    order to extract attributes and node text? Take as an example,
    "<selection name='xyz' count='12'>some-identifier</selection>"

    Up to now, I'm using string operations (look for adjoining characters,
    extract text), but maybe there is a way to do this with Javascript DOM
    commands? The code needs to work with Internet Explorer 6/7.

    Regards,
    Johannes
    Johannes, Jul 21, 2008
    #1
    1. Advertising

  2. Johannes wrote:

    > is there a way to process well-formed XML from a variable (string) in
    > order to extract attributes and node text? Take as an example,
    > "<selection name='xyz' count='12'>some-identifier</selection>"
    >
    > Up to now, I'm using string operations (look for adjoining characters,
    > extract text), but maybe there is a way to do this with Javascript DOM
    > commands? The code needs to work with Internet Explorer 6/7.


    var doc = new ActiveXObject('Msxml2.DOMDocument.3.0');
    if (doc.loadXML(
    "<selection name='xyz' count='12'>some-identifier</selection>"))
    {
    // now you can use the MSXML DOM here e.g.
    var attributes = doc.documentElement.attributes;
    for (var i = 0; i < attributes.length; i++)
    {
    alert(attributes.nodeName + '=' + attributes.nodeValue);
    }
    alert(doc.documentElement.text);
    }
    else
    {
    alert('parse error: ' + doc.parseError.reason);
    }

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Jul 21, 2008
    #2
    1. Advertising

  3. Johannes

    Johannes Guest

    On Jul 21, 1:00 pm, Martin Honnen <> wrote:
    [...]
    >    var doc = new ActiveXObject('Msxml2.DOMDocument.3.0');
    >    if (doc.loadXML(

    [...]

    That's what I've been missing. Thanks very much, Martin.
    Johannes, Jul 21, 2008
    #3
    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. Earl Eiland
    Replies:
    3
    Views:
    518
    Peter Hansen
    May 16, 2005
  2. Trent Mick
    Replies:
    0
    Views:
    473
    Trent Mick
    May 16, 2005
  3. Patrick Kowalzick
    Replies:
    5
    Views:
    459
    Patrick Kowalzick
    Mar 14, 2006
  4. Terrence Brannon
    Replies:
    13
    Views:
    719
    Terrence Brannon
    Nov 7, 2008
  5. Kee Nethery
    Replies:
    12
    Views:
    2,051
    Stefan Behnel
    Jun 27, 2009
Loading...

Share This Page