[英]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.