XML Parser

XML Parser is set of library and program which provide facility to read and manipulate XML document. It is necessary that your XML document is formatted with proper XML structure or schema to read or manipulate XML document by using XML parser. Now a days, all browsers have added in build functionality with extension or their plug-in to read or manipulate XML document.

Look into following diagram to understand it visually.

Main use of XML parser is to read XML document by using library.

All the programming languages or built in parser developed by different company, they have different way or syntax to read/modify or transform XML document into readable format.

Some tools are available on internet to use as parser, but some are free and many are paid.

XMLHttpRequest: XMLHttpRequest object has inbuilt parser.

Expat XML Parser: Specially written in C language.

CodeBeauty Parser: Free XML Viewer, XML Formatter and convert XML document/string into readable format.

Alteryx Parser Tool: The XML parse tool reads in a chunk of Extensible Markup Language (XML) and parse it into individual fields

Microsoft.NET: In .NET framework, there is class “System.Xml.XmlDocument” is used to read or manipulate XML document by using object, methods and its properties.

Let’s understand it with one example. We will read product xml file and display products and its details in table format by using java script and XMLHttpRequest object.

Example:

XML Document:

<?xml version="1.0" encoding="UTF-8"?>
<products>
<product id="1001" >
<name>Sony Xperia R1 Dual</name>
<color>Silver</color>
<price>$650.00</price>
<discount>$20.00</discount>
</product>
<product id="1002" >
<name>Sony Large P1</name>
<color>White</color>
<price>$750.00</price>
<discount>$30.00</discount>
</product>
</products>

Javascript Code:

<!DOCTYPE html>
<html>
<head>
<title>Read XML File by XMLHttpRequest</title>
<meta charset="utf-8" />
<script>
    function LoadData()
    {
        var objXMLHttpRequest = new XMLHttpRequest();
        objXMLHttpRequest.onreadystatechange = function ()
        {
            if(objXMLHttpRequest.readyState == 4 && objXMLHttpRequest.status == 200)
            {
                 var Table = "<table style='border:solid 1px black;'>";
                 Table += "<tr><td><b>Products</tr></td></b>;
                 var XMLString = objXMLHttpRequest.responseXML
                 var Node = XMLString.getElementsByTagName("name");
                 for (i = 0; i < Node.length; i++)
                 {
                    Table += "<tr><td>" + Node[i].childNodes[0].nodeValue + "</tr></td>";
                 }
                Table += "</table">;
                document.getElementById("dvContent").innerHTML = Table;
            }
        };
        objXMLHttpRequest.open("GET", "xmlfiles/products.xml", true);
        objXMLHttpRequest.send();
    }
</script>
</head>
<body>
     <div> <button type="button" onclick=" LoadData()">Display Product Data</button></div><br />
<div id="dvContent"></div>
</body>
</html>

Output:

In above example, we tried to read XML document and displayed product names in table format by using few html lines.