简体   繁体   English

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

[英]How to only delete encoding=UTF8 from xml file

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

So this is how my output file looks like when i try deleting only the "encoding" attribute: 因此,这就是我尝试仅删除“ 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>

What should the output be like: 输出应该是什么样的:

<?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>

So I know I should not delete the encoding part but it is important to me as otherwise I can't work with this file because I use a special tool for the loading. 因此,我知道我不应该删除编码部分,但是这对我很重要,因为否则我将无法使用此文件,因为我使用了一种特殊的加载工具。 The excpected output is a pattern I load in my programm. 预期的输出是我在程序中加载的模式。 After saving it again I get the current output. 再次保存后,得到当前输出。

The xml file has to contain the standalone attribute but not the encoding part. xml文件必须包含独立属性,但不能包含编码部分。

You can make use of ProcessingInstruction which lets you configure any tag you want to append. 您可以使用ProcessingInstruction ,它使您可以配置要附加的任何标签。

In this example I am adding a tag "xml" with the custom data that you specified in your case. 在此示例中,我将添加标记“ xml”以及您在案例中指定的自定义数据。

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

I have created a sample class to test adding the custom xml header. 我创建了一个示例类来测试添加自定义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);
    }
}

Output of hello.xml: hello.xml的输出:

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

UPDATE: There seems to be a problem when using ProcessingInstruction while writing to the file, as it does not indent correctly the xml header from the rest of the xml data. 更新:写入文件时使用ProcessingInstruction时似乎出现问题,因为它无法正确缩进其余xml数据中的xml标头。

Normally, this would not change in anyway the processing the xml, as it is just a human readable thing, not a technical issue. 通常,在处理xml时这不会改变,因为这只是人类可读的事情,而不是技术问题。

But for the ones that do care, here is the alternative to the ProcessingInstruction : 但是对于需要照顾的人,这里是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());
        }
    }

These requirements are stupid, but whatever. 这些要求很愚蠢,但是无论如何。

There is a reason why XML is an industry standard. XML是工业标准是有原因的。 It's a simple text format that can be easily tinkered. 这是一种简单的文本格式,可以很容易地修改。

After you've written your XML file, load it back into a String in memory, add "<?xml version=\\"1.0\\" standalone=\\"yes\\"?>" at the beginning of it, and write that back into your file. 编写XML文件后,将其重新加载到内存中的字符串中,在文件的开头添加"<?xml version=\\"1.0\\" standalone=\\"yes\\"?>" ,然后将其写回到您的文件中。

使用sed

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM