开发者

remove xml declaration from the generated xml document using java

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


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 (among other things) which can be very important.


Add this

format.setOmitXMLDeclaration(true);

Example

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


By using the setOmitXMLDeclaration(true); method from the Format class. I'm not sure but I think it uses jDom lib.

Example (it will display the XML file without the XML declaration of the Document document)

XMLOutputter out= new XMLOutputter(Format.getCompactFormat().setOmitDeclaration(true));
out.output(document, System.out);


This code avoid you xml first line of xml declaration for this use i used xerces-1.4.4.jar

import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.Writer;

import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;


public class TextToXmlFormatter {

    public TextToXmlFormatter() {
    }

    public String format(String unformattedXml) {
        try {
            final Document document = parseXmlFile(unformattedXml);

            OutputFormat format = new OutputFormat(document);
            format.setLineWidth(65);
            format.setIndenting(true);
            format.setIndent(2);
            format.setOmitXMLDeclaration(true);
            Writer out = new StringWriter();
            XMLSerializer serializer = new XMLSerializer(out, format);
            serializer.serialize(document);

            return out.toString();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private Document parseXmlFile(String in) {
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            InputSource is = new InputSource(new StringReader(in));
            return db.parse(is);
        } catch (ParserConfigurationException e) {
            throw new RuntimeException(e);
        } catch (SAXException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        String unformattedXml =
                "YOUR XML STRING";

        System.out.println(new TextToXmlFormatter().format(unformattedXml));
    }

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜