Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML


 
  
 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 

 
Facing Programming Problem?
Ask Questions?, Browse Latest Questions, Question-Answer Guidelines
XML
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

Emitting DOCTYPE Declaration while writing XML File

                         

This Example shows you how to Emmit a DOCTYPE Declaration in a DOM document. JAXP (Java API for XML Processing) is an interface which provides parsing of xml documents. Here the Document BuilderFactory is used to create new DOM parsers.Some of the methods used in code given below for Emitting DOCTYPE Declaration are described below:-

DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance():-This method Creates a DocumentBuilder Factory .DocumentBuilder Factory is a Class that enables application to obtain parser for building DOM trees from XML Document

DocumentBuilder builder = Factory.newDocumentBuilder():-This method creates a DocumentBuilder object with the help of a DocumentBuilderFactory

File file = new File("Document2.xml"):-Creates a new File.

TransformerFactory factory = TransformerFactory.newInstance():-TransformerFactory is a class that is used to create Transformer objects. A TransformerFactory instance can be used to create Transformer and Templates objects.

transformer.transform(source, result):-This function organize data according to a style sheet as we are retrieving it from the File.

t.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, "publicId"):-By this method we can set an output property that will be in effect for the transformation.

Xml code for the program generated is:-

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Company>
    <Employee>
        <name Girish="Gi">Roseindia.net
        </name>
    </Employee>
    <Employee>
        <name Komal="Ko">newsTrack
        </name>
    </Employee>
    <Employee>
        <name Mahendra="Rose">Girish Tewari
        </name>
    </Employee>
</Company>
 

EmittingDOCType.java

/* 
 * @Program for emitting a DOCTYPE Declaration while writing an XML File
   from a DOM Document

 * EmittingDOCType.java 
 * Author:-RoseIndia Team
 * Date:-23-July-2008
 */
import java.io.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;

public class EmittingDOCType {
    public static void main(String[] argsthrows Exception {
        File xmlfile = new File("Document2.xml");
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(xmlfile);
        new EmittingDOCType().emit(doc);
    }

   
public void emit(Document docthrows Exception {
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer t = tf.newTransformer();
        t.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, "publicId");
        t.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "systemId");
        StringWriter writer = new StringWriter();
        StreamResult result = new StreamResult(writer);
        DOMSource source = new DOMSource(doc);
        t.transform(source, result);
        String XString = writer.toString();
        System.out.println(XString);
    }
}

Output of the program:-

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE Company PUBLIC 
"publicId" "systemId">
<Company>
    <Employee>
        <name Girish="Gi">Roseindia.net
        </name>
    </Employee>
    <Employee>
        <name Komal="Ko">newsTrack
        </name>
    </Employee>
    <Employee>
        <name Mahendra="Rose">Girish Tewari
        </name>
    </Employee>
</Company>
  


DownLoad Source Code

                         

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Latest Searches:
how convert hash into
activemq
actionLink
document.this in html
toInt()
requestFocus method
change color in JTabl
jsp applet netbeans
jsf hibernate
jcreator code of calcu
networking
select database with p
java card framework
jsp syntax for select
rational robot
program in java to dis
reflection in java bea
jstl list size
spring jsf integration
breakаÑ?°аÑ?????аÑ
how to print time stam
shoppingcart documenta
midlet inbox example
Selection Sort In Java
prime numbers
types
Create XML file from f
xml
swing frame with menu
draw Polyline
ejb example, jboss
doget
setting values to Requ
Struts HTML EL tags
jsp trim char
java popup window even
Commons-Math: The Jaka
Hibernate EJB3 tutoria
inert date into mysql
tiles in jsp applicati
enterprise architect
Struts 1.x Vs Struts 2
cut,copy,paste on JPan
drop down list
Get Array List in a JS
hibernate installation
appale
year from date
beans servlet
JBOSS 4.0
java bean
drop down in jsp
FileReader java
Logging an Exception i
javascritp checkbox ch
Create table and inser
A�������
myfaces tomahawk
Delete a Specific Row
create file folder alo
distinguish between Re
on clikck radio struts
hashset
struts html:options
java cancel button
server dojo
jdbc type 4 driver
Java Tip 72: Press
DoPost, DoGet , java B
max length 500 chars
populate list box
Linux Caixa MаÐ??ТÐ?
B Tree implementation
iterator in jsp pojo
how to integrate email
commands
java append text to fi
Dreamweaver Getting St
document.getElementsBy
Java example program t
Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.