首页 > 解决方案 > 如何仅从 xml 文件中删除 encoding=UTF8

问题描述

public static void saveXMLDocument(Document xodrDoc, String absoluteFileName) throws Exception{
    //write the content into xml file                    
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    DOMSource source = new DOMSource(xodrDoc);
    StreamResult result = new StreamResult(new File(absoluteFileName));
    transformer.transform(source, result);
}

所以当我尝试只删除“编码”属性时,这就是我的输出文件的样子:

<OpenDRIVE>
<header date="Wed May 23 12:21:34 2018" east="0.0000000000000000e+00" name="" north="0.0000000000000000e+00" revMajor="1" revMinor="4" south="0.0000000000000000e+00" version="1.00" west="0.0000000000000000e+00">
</header>
<road id="7" junction="-1" length="1.0000000000000000e+03" name="">
    <link>
    </link>
    <planView>
        <geometry hdg="0.0000000000000000e+00" length="1.0000000000000000e+01" s="0.0000000000000000e+00" x="0.0000000000000000e+00" y="0.0000000000000000e+00">
            <line/>
        </geometry>
        <geometry hdg="0.0000000000000000e+00" length="22" s="1.0000000000000000e+01" x="1.0000087425259599e+01" y="0.0000000000000000e+00">
            <arc curvature="0.10"/>
        </geometry>
    </planView>

输出应该是什么样的:

<?xml version="1.0" standalone="yes"?>
<OpenDRIVE>
<header revMajor="1" revMinor="4" name="" version="1.00" date="Wed May 23 12:21:34 2018" north="0.0000000000000000e+00" south="0.0000000000000000e+00" east="0.0000000000000000e+00" west="0.0000000000000000e+00">
</header>
<road name="" length="1.0000000000000000e+03" id="7" junction="-1">
    <link>
    </link>
    <planView>
        <geometry s="0.0000000000000000e+00" x="0.0000000000000000e+00" y="0.0000000000000000e+00" hdg="0.0000000000000000e+00" length="1.0000000000000000e+01">
            <line/>
        </geometry>
        <geometry s="1.0000000000000000e+01" x="1.0000087425259599e+01" y="0.0000000000000000e+00" hdg="0.0000000000000000e+00" length="9.9000000000000000e+02">
            <arc curvature="5.0000000000000001e-04"/>
        </geometry>
    </planView>

所以我知道我不应该删除编码部分,但这对我来说很重要,否则我无法使用此文件,因为我使用特殊工具进行加载。预期的输出是我在程序中加载的模式。再次保存后,我得到了当前输出。

xml 文件必须包含独立属性,但不能包含编码部分。

标签: javaxmlencoding

解决方案


您可以利用ProcessingInstruction它来配置您想要附加的任何标签。

在此示例中,我将使用您在案例中指定的自定义数据添加标签“xml”。

ProcessingInstruction newPI = xodrDoc.createProcessingInstruction("xml", "version=\"1.0\" standard=\"yes\"");
            xodrDoc.insertBefore(newPI, xodrDoc.getDocumentElement());

我创建了一个示例类来测试添加自定义 xml 标头。

public class SaveXmlWithCustomXmlHeader {

    public static void main(String[] args) throws Exception {
        String xmlString = "<a><b></b><c></c></a>";

        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder;
        Document document = null;
        try {
            builder = factory.newDocumentBuilder();
            document = builder.parse(new InputSource(new StringReader(xmlString)));
        } catch (Exception e) {
            e.printStackTrace();
        }
        saveXMLDocument(document, "hello.xml");
    }

    public static void saveXMLDocument(Document xodrDoc, String absoluteFileName) throws Exception {
        // add custom xml tag
        ProcessingInstruction newPI = xodrDoc.createProcessingInstruction("xml", "version=\"1.0\" standard=\"yes\"");
        xodrDoc.insertBefore(newPI, xodrDoc.getDocumentElement());

        //write the content into xml file
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        DOMSource source = new DOMSource(xodrDoc);
        StreamResult result = new StreamResult(new File(absoluteFileName));

        transformer.transform(source, result);
    }
}

hello.xml 的输出:

<?xml version="1.0" standard="yes"?><a>
<b/>
<c/>
</a>

更新:在写入文件 时使用似乎存在问题ProcessingInstruction,因为它没有正确缩进其余 xml 数据中的 xml 标头。

通常,无论如何处理 xml 都不会改变,因为它只是人类可读的东西,而不是技术问题。

但对于那些关心的人,这里是替代方案ProcessingInstruction

public static void saveXMLDocument(Document xodrDoc, String absoluteFileName) throws Exception {
        // add custom xml tag
        //write the content into xml file
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        StringWriter writer = new StringWriter();
        writer.append("<?xml version=\"1.0\" standalone=\"yes\"?>").append("\n");
        final DOMSource source = new DOMSource(xodrDoc);
        transformer.transform(source, new StreamResult(writer));
        try (BufferedWriter bufferedWriter = Files.newBufferedWriter(Paths.get(absoluteFileName))) {
            bufferedWriter.write(writer.toString());
        }
    }

推荐阅读