好消息是您不需要生成“缺失的类”,我将在下面通过一个示例进行演示。
架构.xsd
下面是您的 XML 模式的简化版本。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="unqualified">
<xs:element name="get-all-services-partners-request" />
<xs:complexType name="foo">
<xs:sequence>
<xs:element name="bar" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:schema>
富
JAXB (JSR-222) 实现将为每个复杂类型(命名或匿名)创建一个类。在上述 XML 模式中,只有一种复杂类型 ( foo
),因此仅生成模型类。
package forum12990635;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "foo", propOrder = {
"bar"
})
public class Foo {
@XmlElement(required = true)
protected String bar;
public String getBar() {
return bar;
}
public void setBar(String value) {
this.bar = value;
}
}
对象工厂
与命名复杂类型相对应的全局元素对应于@XmlElementDecl
带有注释的类上的注释@XmlRegistry
(参见: http ://blog.bdoughan.com/2012/07/jaxb-and-root-elements.html )。
package forum12990635;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlElementDecl;
import javax.xml.bind.annotation.XmlRegistry;
import javax.xml.namespace.QName;
@XmlRegistry
public class ObjectFactory {
private final static QName _GetAllServicesPartnersRequest_QNAME = new QName("", "get-all-services-partners-request");
public ObjectFactory() {
}
public Foo createFoo() {
return new Foo();
}
@XmlElementDecl(namespace = "", name = "get-all-services-partners-request")
public JAXBElement<Object> createGetAllServicesPartnersRequest(Object value) {
return new JAXBElement<Object>(_GetAllServicesPartnersRequest_QNAME, Object.class, null, value);
}
}
演示
下面是一个如何使用根元素创建文档的示例get-all-services-partners-request
。
package forum12990635;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance("forum12990635");
Foo foo = new Foo();
foo.setBar("Hello World");
ObjectFactory objectFactory = new ObjectFactory();
JAXBElement<Object> jaxbElement = objectFactory.createGetAllServicesPartnersRequest(foo);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(jaxbElement, System.out);
}
}
输出
下面是运行演示代码的输出。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<get-all-services-partners-request xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="foo">
<bar>Hello World</bar>
</get-all-services-partners-request>