views:

668

answers:

2
String root = "RdbTunnels";
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.newDocument();
Element rootElement = document.createElement(root);
document.appendChild(rootElement);   

OutputFormat format = new OutputFormat(document);
format.setIndenting(true);


XMLSerializer serializer = new XMLSerializer(System.out, format);
serializer.serialize(document);

gives the result as following

<?xml version="1.0" encoding="UTF-8"?>
<RdbTunnels/>

but I need to remove the xml declaration from the output how can I do that

+1  A: 

Have you seen OutputKeys as used by Transformer ? Specifically OMIT_XML_DECLARATION.

Note that removing the header is valid in XML 1.0, but you lose character encoding data (amongst other things) which can be very important.

Brian Agnew
I am basically populating database data into *.tbl file which is for reference for some users, It will be used as reference,need to be in xml format and neet not be an xml document.
flash
Character encoding data isn't important for `UTF-8` as this is default for a document with no XML Declaration.
bobince
+2  A: 

Add this

format.setOmitXMLDeclaration(true);

Example

OutputFormat format = new OutputFormat(document);
format.setIndenting(true);
format.setOmitXMLDeclaration(true);
Peter Lindqvist