gpt4 book ai didi

java - 如何阻止 Simple 将 `class` 属性写入 XML 文件?

转载 作者:太空宇宙 更新时间:2023-11-04 11:05:33 25 4
gpt4 key购买 nike

我正在使用Simple读取和写入第 3 方 XML 文件。读取效果很好,但是在编写集合时,库会在结果文件中添加额外的 class 属性,如下所示:

<translation class="java.util.Collections$UnmodifiableMap">
<!-- stuff here-->
</translation>

我的架构不允许这些,我希望有简单的标签,其中仅包含我明确放置在那里的属性,如下所示:

<translation>
<!-- stuff here-->
</translation>

我如何告诉 Simple 停止编写这些内容并猜测它应该使用哪个集合,就像通常那样?

最佳答案

解决方案实际上是 mentioned on project's page ,你必须使用访客。这很简单:

public class ClassAttributeRemoverVisitor implements Visitor {
@Override
public void read(Type type, NodeMap<InputNode> node) throws Exception {
// Do nothing, framework will guess appropriate class on its own
}

@Override
public void write(Type type, NodeMap<OutputNode> node) throws Exception {
OutputNode element = node.getNode();
element.getAttributes().remove("class");
}
}

要使用此访问者,您必须将 VisitorStrategy 添加到 Persister 的构造函数中:

new Persister(new VisitorStrategy(new ClassAttributeRemoverVisitor()))

关于java - 如何阻止 Simple 将 `class` 属性写入 XML 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46514406/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com