使用Java从生成的XML文档中删除XML声明

11
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声明,该怎么做?


3
为什么需要移除它? - Quentin
如果您需要删除XML声明,那么您不需要XML。您需要使用Flash的专有的略微格式化但实际上并非如此的文档解析器。我建议您使用正则表达式。 - Stefan Kendall
2
啊,抱歉,自以为是的专家们,但事实并非如此。即使系统不支持XML头文件,它仍然是XML。 - edthethird
4个回答

18

你是否看到过 Transformer 中使用的 OutputKeys?特别是 OMIT_XML_DECLARATION。

请注意,从 XML 1.0 中删除头部是有效的,但您会失去字符编码数据(以及其他一些重要信息)。


我基本上是将数据库数据填充到*.tbl文件中,该文件供某些用户参考,它将被用作参考,需要以xml格式呈现,但不需要是一个xml文档。 - flash
2
字符编码数据对于UTF-8并不重要,因为这是没有XML声明的文档的默认值。 - bobince

10
添加这个。
format.setOmitXMLDeclaration(true);

例子

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

3
通过使用Format类的setOmitXMLDeclaration(true)方法。 我不确定,但我认为它使用jDom库。
示例(它将显示Document文档的XML文件,但不包含XML声明)
XMLOutputter out= new XMLOutputter(Format.getCompactFormat().setOmitDeclaration(true));
out.output(document, System.out);

0
这段代码可以避免你在 XML 中使用 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));
    }

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接