Process a string like XML

J

Johannes

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
 
M

Martin Honnen

Johannes said:
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);
}
 

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,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top