In this section, you will learn to check and locate (line and column number) an error in your XML document using the DOM APIs. The XML document follows some rules to check its syntax.
Description of program:
This program takes a XML file on the console and it checks whether the given file exists or not. If it exists then it parses the file using the parse() method. DocumentBuilderFactory and DocumentBuilder classes are needed to get a dom parser. All the parsers have inbuilt capability to verify the well-formed ness of a xml file . No you need not to add any extra method for checking wellformed ness .If the xml file is wellformed , the parsing of the xml document get successfully completed , and the program displays a message like "Employee-Detail.xml is well-formed!". Otherwise, it displays an error and exact error location (line and column number).
Here is the XML File: Employee-Detail1.xml
|<?xml version = "1.0" ?>
<Emp_Id> E-001 </Emp_Id>
<Emp_Name> Vinod </Emp_Name>
<Emp_E-mail> [email protected] </Emp_E-mail>
Here is the Java File: DOMLocateError.java
Output of this program:
Enter File name: Employee-Detail1.xml
[Fatal Error] Employee-Detail1.xml:9:1: The end-tag for element type "Employee"
must end with a '>' delimiter.
type: The end-tag for element type "Employee" must end with a '>' delimiter.
Line 9 Column 1
Rs. 20,000 US$ 300
Today: Rs. 10,000 US$150
Course Duration: 30 hrs
Posted on: June 8, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles