如何仅从xml文件中删除encoding = UTF8

时间:2018-07-17 13:49:51

标签: java xml encoding

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

这就是我尝试仅删除“ encoding”属性时输出文件的样子:

<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文件必须包含独立属性,但不能包含编码部分。

3 个答案:

答案 0 :(得分:1)

您可以利用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());
        }
    }

答案 1 :(得分:0)

这些要求很愚蠢,但是随便吧。

XML是行业标准是有原因的。这是一种简单的文本格式,可以很容易地修改。

编写XML文件后,将其重新加载到内存中的字符串中,在文件的开头添加"<?xml version=\"1.0\" standalone=\"yes\"?>",然后将其写回到文件中。

答案 2 :(得分:-1)

使用sed

sed -i 's/encoding=".*?"//' myfile.xml