繁体   English   中英

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

[英]How to stop Simple from writing `class` attributes to XML files?

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

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

我的架构不允许这些,我想拥有纯标签,这些标签仅带有我明确放置在其中的属性,例如:

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

我该如何告诉Simple停止编写这些内容,而只是像往常一样猜测应该使用什么集合?

解决方案实际上是在项目页面上提到的 ,您必须使用Visitor。 很简单:

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

要使用此访问者,您必须向Persister的构造函数添加一个VisitorStrategy:

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

您还可以使用以下简单的解决方法:

@Path("translation")
@ElementMap(inline=true)
Map translation;

暂无
暂无

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

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