保存时如何读写XML文件并将注释节点视为Java中的文本节点 [英] How to read and write XML files and treat the comment nodes as text nodes in Java when saving

查看:49
本文介绍了保存时如何读写XML文件并将注释节点视为Java中的文本节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在读取从外部系统检索到的Java XML文件,然后对其进行处理,最后将其保存在本地,然后再部署回去.

I'm reading an XML file in Java retrieved from an external system, then processing it and eventually save it locally and deploy it back.

外部系统给我一个包含此节点的XML文件:

The external system gives me an XML file that contains this node:

    <customApplications>
        <label><!-- GDPR Management --></label>
        <name>GDPR_Management</name>
    </customApplications>

问题是评论节点.当我读取文件然后将其保存时,结果如下所示:

The problem is the comment node. When I read the file and then just save it, the result looks like this:

    <customApplications>
        <label>
            <!-- GDPR Management -->
        </label>
        <name>GDPR_Management</name>
    </customApplications>

这是个问题,因为当我将文件部署回外部系统时,它现在认为标签具有一些文本内容.因此,我需要与以前相同的结果,即在注释节点周围没有换行符.

Which is a problem, because when I deploy the file back to the external system, it now thinks that the label has some text content. So I need the same result as it was, i.e. without the line breaks around the comment node.

我试图删除所有注释节点,这在部署文件时效果很好,但是该文件也使用git进行版本控制,并且由于在任何时间都可以从外部系统再次检索该文件,因此会产生许多合并冲突.如第一个示例所示,检索到的文件还是带有注释节点.

I tried to remove all the comment nodes, which works well when deploying the file, but the file is also versioned using git and it produces many merge conflict as the file can be at any time retrieved again from the external system (the retrieved file is again with the comment nodes as you can see in the first example).

然后我尝试在保存之前将所有注释节点更改为文本节点.结果再次不可接受,因为标签再次具有一些文本内容:

Then I tried to change all the comment nodes to text nodes before saving. The result is again not acceptable, because the label again has some text content:

    <customApplications>
        <label>&lt;!--  GDPR Management  --&gt;</label>
        <name>GDPR_Management</name>
    </customApplications>

我如何阅读文档:

var docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
var document = docBuilder.parse(inputStream);
document.getDocumentElement().normalize();
var xp = XPathFactory.newInstance().newXPath();
var nl = (NodeList) xp.evaluate("//text()[normalize-space(.)='']", document, XPathConstants.NODESET);
for (int i = 0; i < nl.getLength(); ++i) {
    var node = nl.item(i);
    node.getParentNode().removeChild(node);
}

我如何保存文档:

var result = new StreamResult(outputStream);
var transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
transformer.setOutputProperty(OutputKeys.VERSION, "1.0");
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.transform(new DOMSource(document), result);

我确实需要与第一个示例相同的结果,但是我不关心在处理文件时如何在dom中表示注释节点.

I really need the same result as the first example, but I do not care about how the comment node will be represented in the dom when processing the file.

感谢任何提示!

推荐答案

如果您希望输出与原始输出相同,请不要使用indent ="yes".指定 indent ="yes" 可使序列化程序在所需的任何位置插入空白.

Don't use indent="yes" if you want the output to be identical to the original. Specifying indent="yes" allows the serializer to insert whitespace pretty-well anywhere it wants.

这篇关于保存时如何读写XML文件并将注释节点视为Java中的文本节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆