What is an XML parser? |
In simplest we can say that “XML Parser is the software that reads an XML document, identifies all the XML tags and passes the data to the application”. The work of XML parser is to do syntactic analysis of the given XML document. |
Parsing XML Documents |
To manipulate an XML document, you need an XML parser. The parser loads the document into your computer’s memory. Once the document is loaded, its data can be manipulated using the DOM. The DOM treats the XML document as a tree. You can understand this tree as the XML document tree shown when you the XML document with any browser such as Firefox etc. |
There are some differences between Microsoft’s XML parser and the XML parser used in Mozilla and Opera browsers. In this tutorial we will show you how to create scripts that will work only in Internet Explorer browsers. |
Working with Microsoft XML Parser |
Microsoft’s XML parser is a COM component that comes with Internet Explorer 5 and higher. Once you have installed Internet Explorer, the parser is available to scripts. If you are using Internet Explorer version lower then IE 5, examples in this chapter may not work. |
Microsoft’s XML parser supports all the necessary functions to traverse the node tree, access the nodes and their attribute values, insert and delete nodes, and convert the node tree back to XML. |
Creating an instance of MS XML Parser : |
var xmlDoc = new ActiveXObject(“Microsoft.XMLDOM”) |
Loading an XML document into parser: |
var xmlDoc = new ActiveXObject(“Microsoft.XMLDOM”) xmlDoc.async=”true” xmlDoc.validateOnParse=”true” xmlDoc.load(“simple_xml.xml”) |
The first line of the script above creates an instance of the XML parser. The second line turns off asynchronized loading, to make sure that the parser will not continue execution of the script before the document is fully loaded. The third line tells the parser to load an XML document called “simple_xml.xml”. |
XML Validation |
Validating an XML file against specified DTD through MS XML Parser: |
Source code of “xml_parser_validation.php” |
<html> <body> <script language=”JavaScript”> var xmlDoc = new ActiveXObject(“Microsoft.XMLDOM”) xmlDoc.async=”true” xmlDoc.validateOnParse=”true” xmlDoc.load(“simple_xml.xml”) if(xmlDoc.parseError.errorCode<0) { document.write(“<br>Error Code: “) document.write(xmlDoc.parseError.errorCode) document.write(“<br>Error Reason: “) document.write(xmlDoc.parseError.reason) document.write(“<br>Error Line: “) document.write(xmlDoc.parseError.line) } else { document.write(“<B>Valid XML Document</b><br />”) } </script> </body> </html> |
Source code of “simple_xml.xml” |
<?xml version=”1.0″ encoding=”UTF-7″?> <!DOCTYPE address SYSTEM “simple_xml_dtd.dtd”> <address> <name>Dharmendra Das</name> <address_str>D 210, sector 11, Rohni</address_str> <city>New Delhi</city> <state>Delhi</state> <pin>110000</pin> <company_info>©©right;</company_info> </address> |
Source code of DTD declaration: |
<?xml version=”1.0″ encoding=”UTF-7″?> <!ELEMENT address (name,address_str+,city,state,pin,company_info?)> <!ELEMENT name (#PCDATA)> <!ELEMENT address_str (#PCDATA)> <!ELEMENT city (#PCDATA)> <!ELEMENT state (#PCDATA)> <!ELEMENT pin (#PCDATA)> <!ELEMENT company_info (#PCDATA)> <!ENTITY copy “©”> <!ENTITY copyright “2007, CSITQuestion.com all rights reserved”> |