为了创建Web服务,我获得了许多XSD。它们之间存在关系:一个公共模式形成骨架XML消息,而另一个模式用于传递业务信息。 然后我将jaxb转换为Java类的所有模式。 现在我陷入了困境:有必要将给定的对象(例如,Person类的对象)放入元素中。
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"any"
})
@XmlRootElement(name = "MessagePrimaryContent")
public class MessagePrimaryContent {
@XmlAnyElement
protected Element any;
/**
* Gets the value of the any property.
*
* @return possible object is
* {@link Element }
*/
public Element getAny() {
return any;
}
/**
* Sets the value of the any property.
*
* @param any allowed object is
* {@link Element }
*/
public void setAny(Element any) {
this.any = any;
}
}
Person类
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "person", propOrder = {
"firstName",
"lastName"
})
public class Person {
protected String firstName;
@XmlElement(required = true)
protected String lastName;
public String getFirstName() {
return firstName;
}
/**
* Sets the value of the firstName property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setFirstName(String value) {
this.firstName = value;
}
/**
* Gets the value of the secondName property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getLastName() {
return lastName;
}
/**
* Sets the value of the lastName property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setLastName(String value) {
this.lastName = value;
}
}
据我所知,我必须将其编组到一个文档中,我可以从中获取元素。为了做到这一点,我写了一个特殊的方法。不幸的是它没有成功,所以我认为我做错了什么。 这个方法的一个版本看起来像这样,它不能正常工作。
public Element createPerson(RequestData requestData) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Person.class);
Person person = new Person();
person.setFirstName("Harry");
person.setLastName("Kane");
Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
Marshaller marshaller = jc.createMarshaller();
marshaller.marshal(person, document);
return document.getDocumentElement();}