Creating XML Strings in JavaScript and manipulating(insert/delete) elements

D

debrief

Hya,


How can I create a xml document using javascript and populate it with
content?


thanks in advance
john
 
M

Martin Honnen

How can I create a xml document using javascript and populate it with
content?

With Mozilla, Opera and Safari you can use the W3C DOM Level 2 Core API
to create an XML DOM document and manipulate it:
var xmlDocument = document.implementation.createDocument('', 'root',
null);
var foo = xmlDocument.createElement('foo');
foo.appendChild(document.createTextNode('bar'));
xmlDocument.documentElement.appendChild(foo);

With IE you need to use e.g.
var xmlDocument = new ActiveXObject('Microsoft.XMLDOM');
xmlDocument.appendChild(xmlDocument.createElement('root'));
var foo = xmlDocument.createElement('foo');
foo.appendChild(document.createTextNode('bar'));
xmlDocument.documentElement.appendChild(foo);
 
D

debrief

With Mozilla, Opera and Safari you can use the W3C DOM Level 2 Core API
to create an XML DOM document and manipulate it:
var xmlDocument = document.implementation.createDocument('', 'root',
null);
var foo = xmlDocument.createElement('foo');
foo.appendChild(document.createTextNode('bar'));
xmlDocument.documentElement.appendChild(foo);

With IE you need to use e.g.
var xmlDocument = new ActiveXObject('Microsoft.XMLDOM');
xmlDocument.appendChild(xmlDocument.createElement('root'));
var foo = xmlDocument.createElement('foo');
foo.appendChild(document.createTextNode('bar'));
xmlDocument.documentElement.appendChild(foo);

The code for IE does not work :(

It gives an error message "Type mismatch" on the:

foo.appendChild(...);


any ideas?


Thanks,
 
M

Martin Honnen

The code for IE does not work :(

It gives an error message "Type mismatch" on the:

foo.appendChild(...);

My bad, correct
foo.appendChild(document.createTextNode('bar'));
to
foo.appendChild(xmlDocument.createTextNode('bar'));
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top