Dom Get Values in XML
DOM Introduction in XML
DOM stands for Document Object Model. It is a programming interface that allows scripts and programs to dynamically access and update the content, structure, and style of XML documents (and also HTML documents).
What is DOM in XML?
DOM represents an XML document as a tree structure.
Each node in the tree corresponds to parts of the XML — elements, attributes, text, comments, etc.
You can read, modify, add, or delete parts of the XML using DOM methods.
It allows programs (e.g., JavaScript) to interact with XML documents dynamically.
How DOM Works with XML
Given this XML:
<book id="101"> <title>Effective Java</title> <author>Joshua Bloch</author></book>The DOM tree looks like:
Document ?? Element: book (id="101") ?? Element: title ? ?? Text: "Effective Java" ?? Element: author ?? Text: "Joshua Bloch"Key Concepts
| Term | Description |
|---|---|
| Document | The entire XML file |
| Element Node | A tag in the XML (e.g., <book>) |
| Attribute Node | Attribute of an element (e.g., id="101") |
| Text Node | The text content inside an element |
| Parent/Child | Relationship between nodes in the tree |
| Siblings | Nodes sharing the same parent |
What You Can Do With DOM in XML
Access elements and attributes
Change element values or attributes
Add new elements or attributes
Remove elements or attributes
Navigate through nodes (parent, child, sibling)
Clone nodes to duplicate parts of XML
Example: Accessing XML with DOM in JavaScript
const parser = new DOMParser();const xmlString = `<book id="101"><title>Effective Java</title><author>Joshua Bloch</author></book>`;const xmlDoc = parser.parseFromString(xmlString, "text/xml");// Access <title> element textconst title = xmlDoc.getElementsByTagName("title")[0].textContent;console.log(title); // Outputs: Effective JavaSummary
The DOM is a standardized way to represent and interact with XML documents as objects, allowing dynamic reading and manipulation by programs.
Want me to explain how to traverse or modify XML DOM next?