faqts : Computers : Programming : Languages : JavaScript : XML

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

136 of 182 people (75%) answered Yes
Recently 8 of 10 people (80%) answered Yes

Entry

How can I create an XML document object?

Aug 31st, 2004 06:10
Martin Honnen,


With IE5 you can create the document object with
  var xmlDocument = new ActiveXObject('Microsoft.XMLDOM');
Then you create and set the root element, documentElement, e.g.
  var root = xmlDocument.createElement('gods');
  xmlDocument.documentElement = root;
To this documentElement you can then add further nodes with DOM methods
  var god = xmlDocument.createElement('god');
  god.setAttribute('name', 'Kibo');
  god.setAttribute('home', 'http://www.kibo.com');
  xmlDocument.documentElement.appendChild(god);

With NN6/7, Mozilla, Firebird, and with Opera 7.60 and later you can
create the document object including root element with
  var xmlDocument = 
    document.implementation.createDocument('', 'rootElementName', null)
e.g.
  var xmlDocument = 
    document.implementation.createDocument('', 'gods', null);
Then you use DOM methods to add further elements
  var god = xmlDocument.createElement('god');
  god.setAttribute('name', 'Kibo');
  god.setAttribute('home', 'http://www.kibo.com');
  xmlDocument.documentElement.appendChild(god);