I have object, tree/model/hierarchy, whatever the correct term is. It consists of what could be characterized as a one-to-one mapping of the desired XML.
我有对象,树/模型/层次结构,无论正确的术语是什么。它由可以表征为所需XML的一对一映射的内容组成。
That is i have the following (in a non-standard UML sort of syntax)
这就是我有以下(用非标准的UML语法)
class A {
class B b[*]
class C
class D
}
class B {
class C c[*]
string AttributeFoo = "bar"
}
class C {
string AttributeThis = "is"
}
class D {
string AttributeName = "d"
}
Desired output is something like this:
期望的输出是这样的:
<?xml version="1.0"?>
<a>
<b attribute-foo="bar">
<c attribute-this="is"/>
</b>
<c attribute-this="is"/>
<d attribute-name="d"/>
</a>
What would you propose to be the best, and/or the simplest way of achieving this goal?
您认为什么是实现这一目标的最佳和/或最简单的方法?
6 个解决方案
#1
3
I would look at JAXB because a) you get it in the standard library and b) it isn't that complicated. This code requires Java 6:
我会看看JAXB,因为a)你在标准库中得到它并且b)它并不复杂。此代码需要Java 6:
@XmlRootElement public static class A {
public List<B> b = new ArrayList<B>();
}
public static class B {
public List<C> c = new ArrayList<C>();
@XmlAttribute(name = "attribute-foo") public String attributeFoo = "foo";
}
public static class C {
@XmlAttribute(name = "attribute-this") public String attributeThis = "is";
}
public static void main(String[] args) {
A a = new A();
a.b.add(new B());
a.b.get(0).c.add(new C());
JAXB.marshal(a, System.out);
}
//TODO: getters/setters, error handling and so on
Output:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<a>
<b attribute-foo="foo">
<c attribute-this="is"/>
</b>
</a>
#2
3
The simplest way is to use XStream. See here for an idea what it does. It can be a bit buggy, but for simple tasks its great. For a more comprehensive (and reliable) technology, then JAXB (part of Java6, see javax.xml.bind
) is the better option.
最简单的方法是使用XStream。请看这里了解它的作用。它可能有点儿麻烦,但对于简单的任务它很棒。对于更全面(和可靠)的技术,JAXB(Java6的一部分,请参阅javax.xml.bind)是更好的选择。
#3
3
I think it is the purpose of JAXB (http://jaxb.java.net/) to map Objects to/from XML
我认为JAXB(http://jaxb.java.net/)的目的是将对象映射到XML或从XML映射
#4
2
If you want to use tools, have a look at jaxb. Marshalling/Unmarshalling is what your're doing here, it's a common problem with a lot of solutions available.
如果您想使用工具,请查看jaxb。编组/解编是您在这里所做的事情,这是许多解决方案的常见问题。
I think it's the best (not reinventing the wheel) and it's the simplest (I've done it manually and it's really no fun - object graphs can be cyclic...)
我认为它是最好的(不是重新发明*)而且它是最简单的(我手动完成它并没有什么乐趣 - 对象图可以循环...)
#6
1
In my opinion if your not too bothered about performance I would stay away from Jaxb and take a look at some of the simpler frameworks. If performance is an issue I tend to favour jibx over jaxb for most situations.
在我看来,如果你对性能不太感兴趣,我会远离Jaxb并看看一些更简单的框架。如果性能是一个问题,我倾向于在大多数情况下支持jibx而不是jaxb。
In this situation I tend to use the simple project.
在这种情况下,我倾向于使用简单的项目。
http://simple.sourceforge.net/download/stream/doc/tutorial/tutorial.php#start
Just annotate your object model and away you go..... :-)
只是注释你的对象模型然后你走了..... :-)
@Root
public class Example {
@Element
private String text;
@Attribute
private int index;
public Example() {
super();
}
public Example(String text, int index) {
this.text = text;
this.index = index;
}
public String getMessage() {
return text;
}
public int getId() {
return index;
}
}
#1
3
I would look at JAXB because a) you get it in the standard library and b) it isn't that complicated. This code requires Java 6:
我会看看JAXB,因为a)你在标准库中得到它并且b)它并不复杂。此代码需要Java 6:
@XmlRootElement public static class A {
public List<B> b = new ArrayList<B>();
}
public static class B {
public List<C> c = new ArrayList<C>();
@XmlAttribute(name = "attribute-foo") public String attributeFoo = "foo";
}
public static class C {
@XmlAttribute(name = "attribute-this") public String attributeThis = "is";
}
public static void main(String[] args) {
A a = new A();
a.b.add(new B());
a.b.get(0).c.add(new C());
JAXB.marshal(a, System.out);
}
//TODO: getters/setters, error handling and so on
Output:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<a>
<b attribute-foo="foo">
<c attribute-this="is"/>
</b>
</a>
#2
3
The simplest way is to use XStream. See here for an idea what it does. It can be a bit buggy, but for simple tasks its great. For a more comprehensive (and reliable) technology, then JAXB (part of Java6, see javax.xml.bind
) is the better option.
最简单的方法是使用XStream。请看这里了解它的作用。它可能有点儿麻烦,但对于简单的任务它很棒。对于更全面(和可靠)的技术,JAXB(Java6的一部分,请参阅javax.xml.bind)是更好的选择。
#3
3
I think it is the purpose of JAXB (http://jaxb.java.net/) to map Objects to/from XML
我认为JAXB(http://jaxb.java.net/)的目的是将对象映射到XML或从XML映射
#4
2
If you want to use tools, have a look at jaxb. Marshalling/Unmarshalling is what your're doing here, it's a common problem with a lot of solutions available.
如果您想使用工具,请查看jaxb。编组/解编是您在这里所做的事情,这是许多解决方案的常见问题。
I think it's the best (not reinventing the wheel) and it's the simplest (I've done it manually and it's really no fun - object graphs can be cyclic...)
我认为它是最好的(不是重新发明*)而且它是最简单的(我手动完成它并没有什么乐趣 - 对象图可以循环...)
#5
#6
1
In my opinion if your not too bothered about performance I would stay away from Jaxb and take a look at some of the simpler frameworks. If performance is an issue I tend to favour jibx over jaxb for most situations.
在我看来,如果你对性能不太感兴趣,我会远离Jaxb并看看一些更简单的框架。如果性能是一个问题,我倾向于在大多数情况下支持jibx而不是jaxb。
In this situation I tend to use the simple project.
在这种情况下,我倾向于使用简单的项目。
http://simple.sourceforge.net/download/stream/doc/tutorial/tutorial.php#start
Just annotate your object model and away you go..... :-)
只是注释你的对象模型然后你走了..... :-)
@Root
public class Example {
@Element
private String text;
@Attribute
private int index;
public Example() {
super();
}
public Example(String text, int index) {
this.text = text;
this.index = index;
}
public String getMessage() {
return text;
}
public int getId() {
return index;
}
}