使用java从生成的xml文档中删除xml声明

时间:2010-01-25 15:42:18

标签: java xml xml-serialization

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);

给出结果如下

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

但是我需要从输出中删除xml声明我该怎么做

4 个答案:

答案 0 :(得分:15)

您是否看到了OutputKeys使用的Transformer?具体来说是OMIT_XML_DECLARATION

请注意,删除标题在XML 1.0中有效,但是丢失了字符编码数据(以及其他内容),这些数据非常重要。

答案 1 :(得分:10)

添加此

format.setOmitXMLDeclaration(true);

实施例

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

答案 2 :(得分:2)

使用setOmitXMLDeclaration(true); Format类的方法。 我不确定,但我认为它使用jDom lib。

示例(它将显示没有Document文档的XML声明的XML文件)

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

答案 3 :(得分:0)

此代码避免您为此使用xml x xml声明的第一行,我使用了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));
    }

}