In DOM level 2, specifications were added to allow creating XML documents dynamically. These functions can be used to create XML documents from JavaScript.

Following methods were added in DOM Level 2 which helps in creating XML document:

createDocument()

The createDocument() method creates an XML document of specified type. It takes 3 parameters as inputs:

  • namespaceURI,
  • name,
  • doctype.

The namespaceURI is the namespace URI of the document to be created, the name is the name of the document element to be created, while the doctype is the type of documents to be created. The namespaceURI is usually not passed as it is difficult to manage namespaces from JavaScript and doctype is rarely used. This method returns a new Document object.

Syntax for createDocument() method

var xmldoc = document.implementation.createDocument(“”, “root”, null);

createDocumentType()

The createDocumentType() method creates an empty documentType node. The input parameters are:

  • name,
  • publicId,
  • systemId.

The name is the name of the document type to be created, the publicId is an external subset public identifier (if it is not present, ‘null’ can be passed as the second argument), and the systemId is an external subset public identifier. This function returns a new documentType node.

Syntax for createDocumentType() method

var xmldoc = document.implementation.createDocumentType(“list1”, "-//W3C//DTD SVG 1.0//EN", "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd");

hasFeature()

 The hasFeature() method checks if the DOM implementation has a specific feature and version. It takes feature and version as input paramters. The feature is the feature which has to be checked andthe version is the version of the feature to check against. This function returns a boolean value indicating if the feature is present or not.

Syntax for hasFeature() method

var hasXML = document.implementation. hasFeature(“XML”, “2.0”);

 

›› go to examples ››