Data Models

Tree Structure

An XML document can be represented as a hierarchical tree structure where:

  • The root element is the top-level node.

  • Each element is a node.

  • Nodes can have child nodes (nested elements).

  • Nodes can have attributes and text content.

This tree structure allows easy traversal, querying, and manipulation of XML data.


Example Tree Representation

<library>
  <book id="1">
    <title>XML Basics</title>
    <author>John Doe</author>
  </book>
  <book id="2">
    <title>Advanced XML</title>
    <author>Jane Smith</author>
  </book>
</library>
  • Root node: <library>

  • Child nodes: Two <book> elements

  • Each <book> node has child nodes <title> and <author>, and an attribute id.


Visual Example

library
├── book (id=1)
│   ├── title: XML Basics
│   └── author: John Doe
└── book (id=2)
    ├── title: Advanced XML
    └── author: Jane Smith

Document Object Model

Key Points

  • DOM represents the XML document as nodes in a tree.

  • Nodes include elements, attributes, text, comments, etc.

  • Allows traversal and manipulation of the XML structure.

  • Available in many programming languages (JavaScript, Java, Python, C#, etc.).


Basic DOM Operations (Example in JavaScript)

// Parse XML string into DOM
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(xmlString, "application/xml");

// Access root element
const root = xmlDoc.documentElement;

// Get elements by tag name
const books = xmlDoc.getElementsByTagName("book");

// Read attribute and text content
const firstBookId = books[0].getAttribute("id");
const title = books[0].getElementsByTagName("title")[0].textContent;

DOM Node Types

Node Type
Description

Element Node

An element like <book>

Attribute Node

Attribute of an element

Text Node

Text content inside elements

Comment Node

XML comments

Document Node

The entire XML document

Last updated