XML DOM

For accessing and manipulating documents, a standard is defined by the DOM.

“The W3C Document Object Model (DOM) is a platform and language-neutral interface that allows programs and scripts to dynamically access and update the content, structure, and style of a document.”

For accessing and manipulating HTML documents, the HTML DOM defines a standard way and presents an HTML document as a tree-structure. For accessing and manipulating XML documents, the XML DOM defines a standard way and presents an XML document as a tree-structure. For working with HTML or XML, a good understanding of DOM is necessary.

HTML DOM:

Through the HTML DOM, we can access all the HTML elements.

Example:





Hello World!

Output 1:

Output 2:

Explanation:

In the above example, the value of an HTML element changes with id=”demo”.

XML DOM:

How to get, change, add, and delete XML elements is defined by the XML DOM as a standard. Through the XML DOM, we can access all the XML elements.

Books.xml:




  
    ABC
    Author Name
    2020
    100.00
  

  
    XQuery Book
    Author 1
    Author 2
    Author 3
    Author 4
    2004
    350.00
  


Example:





Output:

Explanation:

In the above example, we are loading a text string into an XML DOM object. We then extract the info from it with JavaScript. The below line of code retrieves the text value of the first <title> element in an XML document:

txt = xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue;