Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Xml Dom Adding DOCTYPE to a XML File

Related Tutorials


 
 

Share on Google+Share on Google+

Adding DOCTYPE to a XML File

Advertisement
In this section, you will learn to add a DOCTYPE to your XML file using the DOM APIs.

Adding DOCTYPE to a  XML File

     

In this section, you will learn to add a DOCTYPE to your XML file  using the DOM APIs. 

Description of program:

The following program helps to add a  DOCTYPE in your XML  file. Program takes a XML file name on the console and it checks, the given file exists or not. If the given file exists then it is  parsed using the parse() method and a Document object treee 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 systemId and publicId to the  DOCTYPE in the XML file. 

Here is the XML File: Employee-Detail.xml

<?xml version = "1.0" ?>
<Employee-Detail>

<Employee>
<Emp_Id> E-001 </Emp_Id>
<Emp_Name> Vinod </Emp_Name>
<Emp_E-mail> Vinod1@yahoo.com </Emp_E-mail>
</Employee>

<Employee>
<Emp_Id> E-002 </Emp_Id>
<Emp_Name> Amit </Emp_Name>
<Emp_E-mail> Amit2@yahoo.com </Emp_E-mail>
</Employee>

<Employee>
<Emp_Id> E-003 </Emp_Id>
<Emp_Name> Deepak </Emp_Name>
<Emp_E-mail> Deepak3@yahoo.com </Emp_E-mail>
</Employee>

</Employee-Detail>

Here is the Java File: AddDocType.java

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 AddDocType{
  static public void main(String[] args){
  try{
  BufferedReader bf = new BufferedReader(
 
new InputStreamReader(System.in));
  System.out.print("Enter XML file name: ");
  String xmlFile = bf.readLine();
  System.out.println();
  File file = new File(xmlFile);
  if (file.exists()){
  DocumentBuilderFactory factory = 
  DocumentBuilderFactory.newInstance
();
  DocumentBuilder builder = factory.newDocumentBuilder();
  Document doc = builder.parse(xmlFile);
//  Create transformer
  Transformer tFormer = 
  TransformerFactory.newInstance
().newTransformer();
//  Set system id
  tFormer.setOutputProperty(
  OutputKeys.DOCTYPE_SYSTEM, 
"systmId");

  Source source = new DOMSource(doc);
  Result result = new StreamResult(System.out);
  tFormer.transform(source, result);
  System.out.println();
  }
  else{
  System.out.println("File not found!");
  }
  }
  catch (Exception e){
  e.getMessage();
  }
  }
}

Download this example.

Output of this program:

XML docType SystemId and publicId

C:\vinod\xml>java AddDocType
Enter XML file name: Employee-Detail.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
  <!DOCTYPE Employee-Detail
PUBLIC "publicId" "systmId">
<Employee-Detail>
<Employee>
<Emp_Id> E-001 </Emp_Id>
<Emp_Name> Vinod </Emp_Name>
<Emp_E-mail> Vinod1@yahoo.com </Emp_E-mail>
</Employee>
<Employee>
<Emp_Id> E-002 </Emp_Id>
<Emp_Name> Sushil </Emp_Name>
<Emp_E-mail>Sushil@yahoo.com </Emp_E-mail>
</Employee>
<Employee>
<Emp_Id> E-003 </Emp_Id>
<Emp_Name> Amit </Emp_Name>
<Emp_E-mail> Amit@yahoo.com </Emp_E-mail>
</Employee>
</Employee-Detail>
Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 8, 2007

Related Tutorials

Discuss: Adding DOCTYPE to a XML File   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com