JAVA XML

JavaXml Projects

JavaXml Project 1

JavaXml Examples

JavaXml EXAMPLE

Adding DOCTYPE in XML Document
Previous Home Next

In this program, we will be learn that how we can add a DOCTYPE to your XML file using the DOM application programming interfaces. This program is help to add a DOCTYPE in your XML file. Its Program takes a XML document name and checks it, the given file is exists or not. If the given file exists then it is parsed using the parse() method and a Document object tree is created . Abstract class Transformer is used to transform a source tree into a xml file The setOutputProperty() method invokes to the Transformer object and sets the system Id and public Id to the DOCTYPE in the XML file.

Example

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@yahoo.com </stu_E-mail>
</Student>
<Student>
<stu_Id> s-002 </stu_Id>
<stu_Name> Ajit </stu_Name>
<stu_E-mail> Ajit2@gmail.com </stu_E-mail>
</Student>
<Student>
<stu_Id> s-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: Student_Detail.java

 

package r4r;
import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;
import javax.xml.transform.*; 
import javax.xml.transform.dom.DOMSource; 
import javax.xml.transform.stream.StreamResult;
public class studentrecord {
	static public void main(String[] args){
		  try{
		  BufferedReader br = new BufferedReader(
		  new InputStreamReader(System.in));
		  System.out.print("Enter the XML file name: ");
		  String str = br.readLine();
		  System.out.println();
		  File file = new File(str);
		  if (file.exists()){
		  DocumentBuilderFactory fact = 
		  DocumentBuilderFactory.newInstance();
		  DocumentBuilder build = fact.newDocumentBuilder();
		  Document docu = build.parse(str);
		  Transformer tf = 
		  TransformerFactory.newInstance().newTransformer();
		  tf.setOutputProperty(
		  OutputKeys.DOCTYPE_SYSTEM, "systmId");
		  Source src = new DOMSource(docu);
		  Result result = new StreamResult(System.out);
		  tf.transform(src, result);
		  System.out.println();
		  }
		  else{
		  System.out.println("File not found!");
		  }
		  }
		  catch (Exception e){
		  e.getMessage();
		  }
		  }
}
Previous Home Next

Tolal:0 Click:

Show All Comments

Did not find what you were looking for leave your name and message. We will revert within 24 hours
Name:
eMail:
Comment / Feedback: