代码之家  ›  专栏  ›  技术社区  ›  gronostaj kenorb

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

  •  1
  • gronostaj kenorb  · 技术社区  · 7 年前

    我正在使用 Simple 读取和写入第三方XML文件。阅读效果很好,但在写作藏书时,图书馆会额外投入 class

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

    我的模式不允许这些,我希望有一个简单的标记,其中只包含我显式放在那里的属性,比如:

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

    2 回复  |  直到 7 年前
        1
  •  0
  •   gronostaj kenorb    7 年前

    解决方案实际上是 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");
        }
    }
    

    new Persister(new VisitorStrategy(new ClassAttributeRemoverVisitor()))
    
        2
  •  0
  •   Taras Kohut    7 年前

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