Home » 
        XML Tutorial
    
    XML Parser
    
    
    
    
        
            By IncludeHelp Last updated : December 29, 2024
        
    
    What is an XML Parser?
An XML parser is a software library or tool that reads XML documents and provides a structured representation, allowing programs to interact with the data. It ensures that the document is well-formed and validates its structure against predefined rules (if applicable).
Types of XML Parsers
There are two primary types of XML parsers:
- DOM (Document Object Model) Parser
 
- SAX (Simple API for XML) Parser
 
Let's explore these in detail.
1. DOM (Document Object Model) Parser
The DOM parser reads the entire XML document into memory and creates a tree structure that can be traversed and manipulated. This makes it ideal for scenarios requiring random access to XML elements.
Features of DOM Parser
- Creates a tree-based structure in memory.
 
- Supports both reading and writing operations.
 
- Simple and intuitive API.
 
Advantages
- Easy to use for small XML documents.
 
- Enables random access to different parts of the document.
 
Disadvantages
- Memory-intensive as the entire document is loaded into memory.
 
- Not suitable for very large XML files.
 
2. SAX (Simple API for XML) Parser
SAX is an event-driven parser that reads XML data sequentially. Unlike DOM, it does not load the entire document into memory.
Features of SAX Parser
- Event-based parsing (e.g., start and end tags).
 
- Does not create a tree structure in memory.
 
Advantages
- Memory efficient as it processes data on-the-fly.
 
- Suitable for large XML files.
 
Disadvantages
- Does not allow random access to data.
 
- Complex implementation for certain tasks.
 
Parsing XML with JavaScript
Modern web browsers provide built-in support for parsing XML documents using JavaScript. Below are examples demonstrating how to work with XML using the DOMParser and XMLHttpRequest objects.
Example 1: Parsing an XML String
This example demonstrates how to parse a string containing XML data into an XML DOM object and extract information from it.
<!DOCTYPE html>
<html>
<body>
  <p id="demo"></p>
  <script>
    var text, parser, xmlDoc;
    text = `
      <library>
        <book>
          <title>Introduction to AI</title>
          <author>Amitabh Sharma</author>
          <year>2021</year>
        </book>
        <book>
          <title>Data Structures</title>
          <author>Rajesh Verma</author>
          <year>2018</year>
        </book>
      </library>`;
    parser = new DOMParser();
    xmlDoc = parser.parseFromString(text, "text/xml");
    document.getElementById("demo").innerHTML =
      xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue;
  </script>
</body>
</html>
Explanation
- Define XML Data: The text variable holds an XML string.
 
- Parse the String: Use 
DOMParser to convert the string into an XML DOM object. 
- Extract Data: Access specific elements (e.g., 
<title>) using getElementsByTagName. 
Example 2: Using XMLHttpRequest
This example demonstrates how to fetch and parse an external XML file using XMLHttpRequest:
<!DOCTYPE html>
<html>
<body>
  <p id="demo"></p>
  <script>
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function() {
      if (this.readyState == 4 && this.status == 200) {
        var xmlDoc = this.responseXML;
        var output = "";
        var books = xmlDoc.getElementsByTagName("book");
        for (var i = 0; i < books.length; i++) {
          var title = books[i].getElementsByTagName("title")[0].childNodes[0].nodeValue;
          var author = books[i].getElementsByTagName("author")[0].childNodes[0].nodeValue;
          output += "<b>Title:</b> " + title + "<br><b>Author:</b> " + author + "<br><br>";
        }
        document.getElementById("demo").innerHTML = output;
      }
    };
    xmlhttp.open("GET", "books.xml", true);
    xmlhttp.send();
  </script>
</body>
</html>
books.xml
<library>
  <book>
    <title>Advanced Mathematics</title>
    <author>Kavita Das</author>
  </book>
  <book>
    <title>Machine Learning</title>
    <author>Vikram Patel</author>
  </book>
</library>
Explanation
- Send Request: Use 
XMLHttpRequest to fetch the XML file. 
- Parse Response: Access the 
responseXML property to get the XML DOM object. 
- Extract Data: Loop through 
<book> elements and extract titles and authors. 
	
    
    
    
    
  
    Advertisement
    
    
    
  
  
    Advertisement