我很难理解 Java Simple 。它看起来很酷,但我不知道如何使用它。
我找不到“Hello World”示例来演示各个部分如何组合在一起。我正在寻找这样的东西;
预期输出
<durr/>
演示.java
public class Demo {
public static void main(String[] args) {
String str = new Foo();
System.out.println(str);
}
}
Foo.java
import org.simpleframework.xml.Root;
@Root
public class Foo{
// Something
}
编译并运行
javac Demo.java Foo.java
java Demo
摘要
最简单的demo是什么,可以输出<durr/>
使用简单吗?
最佳答案
直接引用simple's documentation :
To serialize an instance of the above object a Persister is required. The persister object is then given an instance of the annotated object and an output result, which is a file in this example. Other output formats are possible with the persister object.
所以基本上,我想说你的演示类可以用以下方式编写:
import java.io.File;
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;
public static void main(String[] args) throws Exception {
Foo foo = new Foo();
Serializer serializer = new Persister();
File result = new File("example.xml"); // XML OUTPUT WILL BE WRITTEN HERE
serializer.write(foo, result);
}
再一次,根据文档,我想说这将生成 <foo/>
tag ,因为它是你的类(class)名称,将其更改为 <duur/>
输出您必须在 Foo
的根标记中重新定义名称类,像这样:
import org.simpleframework.xml.Root;
@Root(name="durr")
public class Foo{
// Something
}
关于java - Java Simple 需要 `Hello World` 示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13754550/