- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我正在使用 org.simpleframework.xml (http://simple.sourceforge.net/) 将 Java 对象序列化为 XML。
我想添加的是基于 Java 对象中的注释,在生成的 XML 中添加一个注释区域。
例如,我想编写一些 Java 对象,例如:
@Root(name = "myclass")
public class MyClass {
@Element(required=true)
@Version(revision=1.1)
@Comment(text=This Element is new since, version 1.1, it is a MD5 encrypted value)
private String activateHash;
}
生成的 xml 如下所示:
<myclass version="1.1">
<!-- This Element is new since, version 1.1, it is a MD5 encrypted value -->
<activateHash>129831923131s3jjs3s3jjk93jk1</activateHash>
</myclass>
在他们的文档中有一个关于如何编写将在 xml 中写入评论的访问者的示例: http://simple.sourceforge.net/download/stream/doc/tutorial/tutorial.php#intercept
但是:我怎样才能将访客附加到策略?
此外,simpleframework 的访问者概念不允许访问原始解析类。在访问者中只有一种方法可以覆盖:
public void write(Type type, NodeMap<OutputNode> node) { ... }
=> OutputNode 不让我有机会阅读我正在解析的元素的注释。那么应该如何访问属性的Annotations。
谢谢!
塞巴斯蒂安
最佳答案
截至 2012-11-05 的更新:
org.simpleframework.xml作者的回答:这行得通
package org.simpleframework.xml.strategy;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import org.simpleframework.xml.Default;
import org.simpleframework.xml.Root;
import org.simpleframework.xml.ValidationTestCase;
import org.simpleframework.xml.core.Persister;
import org.simpleframework.xml.stream.InputNode;
import org.simpleframework.xml.stream.NodeMap;
import org.simpleframework.xml.stream.OutputNode;
public class CommentTest extends ValidationTestCase {
@Retention(RetentionPolicy.RUNTIME)
private static @interface Comment {
public String value();
}
@Root
@Default
private static class CommentExample {
@Comment("This represents the name value")
private String name;
@Comment("This is a value to be used")
private String value;
@Comment("Yet another comment")
private Double price;
}
private static class CommentVisitor implements Visitor {
public void read(Type type, NodeMap<InputNode> node) throws Exception {}
public void write(Type type, NodeMap<OutputNode> node) throws Exception {
if(!node.getNode().isRoot()) {
Comment comment = type.getAnnotation(Comment.class);
if(comment != null) {
node.getNode().setComment(comment.value());
}
}
}
}
public void testComment() throws Exception {
Visitor visitor = new CommentVisitor();
Strategy strategy = new VisitorStrategy(visitor);
Persister persister = new Persister(strategy);
CommentExample example = new CommentExample();
example.name = "Some Name";
example.value = "A value to use";
example.price = 9.99;
persister.write(example, System.out);
}
}
更新于 2012-11-01 20:16
这是似乎能达到预期效果的解决方法 - 必要的 FieldHelper 在 ( Get the value of a field, given the hierarchical path) 中描述
/**
* write according to this visitor
*/
public void write(Type type, NodeMap<OutputNode> node) {
OutputNode element = node.getNode();
Class ctype = type.getType();
String comment = ctype.getName();
if (!element.isRoot()) {
FieldHelper fh = new FieldHelper();
element.setComment(comment);
try {
if (type.getClass().getSimpleName().startsWith("Override")) {
type = (Type) fh.getFieldValue(type, "type");
}
if (type.getClass().getSimpleName().startsWith("Field")) {
Field field = (Field) fh.getFieldValue(type, "field");
System.out.println(field.getName());
Comment commentAnnotation = field.getAnnotation(Comment.class);
if (commentAnnotation != null) {
element.setComment(commentAnnotation.value());
}
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
这是我在这方面取得的进展。不幸的是,它没有按预期工作。我已经给 Simpleframwork for XML 的作者写了一封电子邮件。
/**
* write according to this visitor
*/
public void write(Type type, NodeMap<OutputNode> node) {
OutputNode element = node.getNode();
Class ctype = type.getType();
String comment = ctype.getName();
if (!element.isRoot()) {
Comment commentAnnotation = type.getAnnotation(Comment.class);
if (commentAnnotation!=null)
element.setComment(commentAnnotation.value());
else
element.setComment(comment);
}
}
@Override
public void read(Type type, NodeMap<InputNode> nodeMap) throws Exception {
}
}
我这样声明 Comment 注释:
package com.bitplan.storage.simplexml;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.annotation.ElementType;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Comment {
String value();
}
然后可以像这样使用:
@Comment("this is the unique identifier")
private long id;
可以像这样添加访问者:
/**
* get Serializer
*
* @return
*/
public Serializer getSerializer() {
Serializer serializer = null;
Strategy strategy=null;
VisitorStrategy vstrategy=null;
if ((idname != null) && (refname != null)) {
strategy = new CycleStrategy(idname, refname);
}
CommentVisitor cv=new CommentVisitor();
if (strategy==null) {
vstrategy=new VisitorStrategy(cv);
} else {
vstrategy=new VisitorStrategy(cv,strategy);
}
serializer = new Persister(vstrategy);
return serializer;
}
关于java - 如何使用将注释写入生成的 XML 的注释来扩展 org.simpleframework.xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12916198/
我正在尝试使用 Retrofit 从 URL 获取书籍 xml 数据。但是当应用程序运行时,它显示错误。我是新来的所以请帮助我。这是错误信息: 02-20 23:06:37.943 23835
在 Android 上,我使用 SimpleFramework 来解析传入的 XML 并创建适当的对象(之后将其保存到 DB...) XML 的一部分如下所示: 00300001-0000-
我正在尝试使用 simpleframework 序列化第三方 (parasoft) xml 报告。
我有一个 XML 字符串,其属性包含整数值: 还有 Simple Framework 类描述了这个item: @Root public static class Item { @Attrib
我在一个项目中使用 simpleframework ( http://simple.sourceforge.net/ ) 来满足我的序列化/反序列化需求,但在处理空/空字符串值时它没有按预期工作(好吧
我收到一个 XML 响应,其属性包含以下值: Wed Sep 05 10:56:13 CEST 2012 我在我的模型类中定义了一个带注释的字段: @Attribute(name = "regDate
我将我的库模块上传到 jcenter,并将这个模块用于我的应用程序项目。 我尝试构建我的应用程序它返回一个错误。 我搜索了这个问题,这个问题是由于知道什么是简单的框架。 我 必须同时使用这个库 我的库
我正在使用简单框架进行 XML 序列化/反序列化,虽然第一个很简单,但我对后者有疑问。因此,我从服务器收到 XML 响应,如下所示:
img1 img2 img3 img4 img5 img6 我有这样的内容的xml(不是真实的,只是为了描述我的问题) 想要通过 simplefr
所以我试图在元素没有值时设置一个空属性。 所以我的类看起来像这样: @Order(attributes = { "name" }) public class TypeNumber { @Att
我有一个大型 xml 树,其结构如下: 1 Test2 -1 Test1 我正在尝
我使用 SimpleFramework 来创建 XML。一切正常,期待一件“小”事。当我的列表之一为空时,SimpleFramework 会不断添加列表的标签(当然是空的)。当空标签为 null 或
我正在尝试使用 Simpleframework 将 XML 绑定(bind)到 POJO但它无法正常工作,我无法发现错误。这个结构与 JAXB 完美配合,我只是将注释更改为 Simpleframewo
我有这个代码: @ElementList(name="package", required=false, inline=true) private Hashtable packages; 这给了我
我正在尝试使用 org.simpleframework.xml.ElementMap 将以下 XML 映射到我的 Java 类: blahblah one
在经历了很多挫折之后,我来到这里寻求帮助。我正在使用 org.simpleframework.xml 来解析 Android 中的 RSS 提要。尝试解析 xml 文件时出现以下错误: org.sim
我有一个使用 SimpleFramework 的 Android 应用程序用于 XML 序列化。该应用程序在我测试过的所有真实设备上运行良好,没有任何延迟,但在模拟器上运行时,垃圾收集器会在每次启动该
我决定使用 Simple XML serialization并陷入了基本问题。我正在尝试将 java.util.UUID 类实例序列化为这个小类中的最终字段: @Root public class I
我正在使用 org.simpleframework.xml (http://simple.sourceforge.net/) 将 Java 对象序列化为 XML。 我想添加的是基于 Java 对象中的
我正在对 soap 服务使用改造,但出现元素“主体”没有 .. 错误。我不擅长用 SOAP :)这是我的回应和我的对象 %%UmVxdWVzdElzVmFsaWQyOT
我是一名优秀的程序员,十分优秀!