我正在尝试编组我设计的一些类,使用标准的JAXB,这些类都有void构造函数,这是我第一次尝试使用JAXB或编组/ unmarhslling在任何语言中,但据我所知,JAXB应该是能够在没有XSD的情况下对其进行编组。
课程如下:
@XmlRootElement(name="place")
class Place {
@XmlAttribute
//various fields and get set methods
public Place() {
}
}
@XmlRootElement(name="Arc")
class Arc {
// various fields and get set methods
@XmlAttribute
Place p;
public setPlace(Place p) {
// ...
}
public Arc() {
}
}
@XmlRootElement(name="Transition")
class Transition {
Arc[] a;
public Transition() {
}
}
我可以执行Place
课程而不是Arc
课程,Transition
我甚至没有尝试过,这些课程有@XMLPropriety
个标签,但是当它到达时嵌套的Place
类JAXB似乎也不了解要映射它的XML对象。
如果有另一个标签,我应该用于嵌套类,或者我忽略了另一个错误?
答案 0 :(得分:6)
使用任何JAXB (JSR-222)实现来处理嵌套类都没有什么特别之处。下面是一个完整的示例,其中只使用了一个@XmlRootElement
注释:
<强>过渡强>
package forum13159089;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
class Transition {
Arc[] a;
public Arc[] getA() {
return a;
}
public void setA(Arc[] a) {
this.a = a;
}
}
<强>电弧强>
package forum13159089;
class Arc {
Place p;
public Place getPlace() {
return p;
}
public void setPlace(Place p) {
this.p = p;
}
}
<强>放置强>
package forum13159089;
class Place {
}
<强>演示强>
package forum13159089;
import java.io.File;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Transition.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("src/forum13159089/input.xml");
Transition transition = (Transition) unmarshaller.unmarshal(xml);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(transition, System.out);
}
}
<强> input.xml中/输出强>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<transition>
<a>
<place/>
</a>
<a>
<place/>
</a>
</transition>
了解更多信息
注意: @XMLProperty
不是JAXB注释。
答案 1 :(得分:5)
这段代码对我有用。请检查。
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class First {
@XmlElement
private String name;
@XmlElement
private String surname;
@XmlElement
private String address;
getters and setters
}
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Second {
@XmlElement
private String address1;
@XmlElement
private String address2;
@XmlElement
private String address3;
@XmlElement
private First first;
getters and setters
}
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Forth {
@XmlElement
private String address6;
@XmlElement
private String address7;
@XmlElement
private Second[] array = new Second[2];
getters and setters
}
public static void main(String arsg[]) throws Exception
{
First first = new First();
first.setName("Kshitij");
first.setSurname("Solanki");
first.setAddress("Nadiad");
Second second = new Second();
second.setAddress1("Kshiutij_1");
second.setAddress2("Kshiutij_2");
second.setAddress3("Kshiutij_3");
second.setFirst(first);
Second second1 = new Second();
second1.setAddress1("Kshiutij_1");
second1.setAddress2("Kshiutij_2");
second1.setAddress3("Kshiutij_3");
second1.setFirst(first);
Second[] arra = {second, second1};
Forth forth = new Forth();
forth.setAddress6("kjhgaksjfsadf");
forth.setAddress7("sdlkfsdf");
forth.setArray(arra);
JAXBContext context = JAXBContext.newInstance(Forth.class);
Marshaller marshaller = context.createMarshaller();
StringWriter stringWriter = new StringWriter();
marshaller.marshal(forth, stringWriter);
System.out.println(stringWriter.toString());
}
对不起,如果我不明白你的问题。