1

我想在标头中生成 1.1 作为版本的 xml 文件,因为在使用 1.0 版本解析我的 xml 文件时我得到 SaxparserExceptions:

"Character reference "&#3" is an invalid XML character". 

当我手动更改标题时,我没有收到任何错误。通过 outputkeys 更改版本似乎不起作用。该文件仍然有错误的标题:

"<?xml version="1.0" encoding="UTF-8" standalone="no"?>"

示例代码:

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

        TransformerFactory transformerFactory = TransformerFactory
                .newInstance();
        Transformer transformer = transformerFactory
                .newTransformer();

        transformer.setOutputProperty(
                OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.VERSION, "1.1");
        transformer
                .setOutputProperty(
                        "{http://xml.apache.org/xslt}indent-amount",
                        "2");


        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(
                 "processed_.xml");
        transformer.transform(source, result);

    } catch (TransformerConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (TransformerException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
4

2 回答 2

1

Xalan 可能不支持 XML 1.1。试试撒克逊语。

于 2014-09-08T21:50:31.700 回答
0

doc.setXmlVersion("1.1"); 为我工作。

于 2019-12-17T14:03:11.180 回答