JAVA XML

JavaXml Projects

JavaXml Project 1

JavaXml Examples

JavaXml EXAMPLE

adplus-dvertising
XML Well_Formed
Previous Home Next

In this program, we will be learn to check the well-formed-ness of a XML using the DOM interface. A well-formed XML document must follow the xml syntax rules.

Here is the XML File: Student_Detail.xml

<?xml version = "1.0" ?>
<Student_Detail>
<Student>
<stu_Id> s-001 </stu_Id>
<stu_Name> samir </stu_Name>
<stu_E-mail> sam1@gmail.com </stu_E-mail>
</Student>
<Student>
<stu_Id> s-002 </stu_Id>
<stu_Name> Ajit </stu_Name>
<stu_E-mail> Ajit2@yahoo.com </stu_E-mail>
</Student>
<Student>
<stu_Id> E-003 </stu_Id>
<stu_Name> Deepti </stu_Name>
<stu_E-mail> Deep3@yahoo.com </stu_E-mail>
</Student>
</Student-Detail>

Here is the Java File: FormateCheck.java

package r4r;
import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;
public class formatecheck {
static public void main(String[] arg){
try{
BufferedReader br = new BufferedReader(
new InputStreamReader(System.in));
System.out.print("Enter the File name: ");
String xmlFile = br.readLine();
File file = new File(xmlFile);
if(file.exists()){
try {
DocumentBuilderFactory docFact =
DocumentBuilderFactory.newInstance();
DocumentBuilder build = docFact.newDocumentBuilder();
InputSource inputs = new InputSource(xmlFile);
InputStream is = null;
Document docu = build.parse(is);
System.out.println(xmlFile + " is well-formed!");
}
catch (Exception e) {
System.out.println(xmlFile + " isn't well-formed!");
System.exit(1);
}
}
else{
System.out.print("File not found!");
}
}
catch(IOException io){
io.printStackTrace();
}
}
}
Previous Home Next