Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add Doctype in XML document using DOM (JAVA)

I have created a XML document using DOM in java. I am unable to add doctype. I want the doctype like this.

<!DOCTYPE IndInfo PUBLIC "EDAFileSomething" "EDAFileSomething_2_0.dtd">

Here is the document creation code.

DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.newDocument();

Here is the Transformer Object Code.

TransformerFactory transformerFactory = TransformerFactory.newInstance();
            Transformer transformer = null;
            try {
                transformer = transformerFactory.newTransformer();
            } catch (TransformerConfigurationException ex) {
                Logger.getLogger(Reader.class.getName()).log(Level.SEVERE, null, ex);
            }
            DOMSource source = new DOMSource(doc);
            try {

                StreamResult result = new StreamResult(System.out);
                transformer.transform(source, result);
            } catch (TransformerException ex) {
                Logger.getLogger(Reader.class.getName()).log(Level.SEVERE, null, ex);
            }

            System.out.println("File saved!");
like image 603
wali Avatar asked Nov 25 '12 17:11

wali


People also ask

Is DOCTYPE required in XML?

DOCTYPE> is not mandatory in XML.

Can we create an XML document using DOM parser?

In short the basic steps one has to take in order to create an XML File withe a DOM Parser are: Create a DocumentBuilder instance. Create a Document from the above DocumentBuilder . Create the elements you want using the Element class and its appendChild method.


2 Answers

You can construct the doctype with the DOM and set the doctype as an output property.

TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
DOMImplementation domImpl = document.getImplementation();
DocumentType doctype = domImpl.createDocumentType("doctype",
    "-//Oberon//YOUR PUBLIC DOCTYPE//EN",
    "YOURDTD.dtd");
transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, doctype.getPublicId());
transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, doctype.getSystemId());
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(new File(database));
transformer.transform(source, result);
like image 137
Adam Avatar answered Sep 22 '22 14:09

Adam


If you get an instance of DOMImplementation by invoking the getDOMImplementation() method on the DocumentBuilder, you can use the createDocument method to create a new Document with the specified doctype.

It also has a createDocumentType method for creating a DocumentType object

Refer to http://docs.oracle.com/javase/1.5.0/docs/api/org/w3c/dom/DOMImplementation.html and http://docs.oracle.com/javase/1.5.0/docs/api/javax/xml/parsers/DocumentBuilder.html for more info.

like image 30
Magnus Avatar answered Sep 25 '22 14:09

Magnus