我有一个xsd,其中每个元素名称及其复杂类型名称相同。当我使用JAXB从xsd生成Java时,这会产生问题。此xsd由外部服务提供商提供。 XSD是错误的还是我在生成xsd时做错了什么。以下是xsd:
的示例<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="unqualified">
<xs:element name="Discounts" type="Discounts"/>
<xs:complexType name="Discounts">
<xs:complexContent>
<xs:extension base="listWithSizeAttribute">
<xs:sequence>
<xs:element maxOccurs="unbounded" minOccurs="0" name="Discounttest" type="Discounttest"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
每当我使用JAXB生成Java对象时,我都会收到错误,其中包含重复元素折扣。整个xsd充满了这样的元素。因此,我觉得我身边出了点问题。
谢谢, 阿克沙伊
答案 0 :(得分:0)
XML架构
全局元素和复杂类型具有相同名称时没有问题。我已经刷新了你的XML模式以使其有效,以便我可以在其上运行模式编译。
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
attributeFormDefault="unqualified" elementFormDefault="unqualified">
<xs:element name="Discounts" type="Discounts" />
<xs:complexType name="listWithSizeAttribute" />
<xs:complexType name="Discounttest" />
<xs:complexType name="Discounts">
<xs:complexContent>
<xs:extension base="listWithSizeAttribute">
<xs:sequence>
<xs:element maxOccurs="unbounded" minOccurs="0"
name="Discounttest" type="Discounttest" />
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:schema>
<强>折扣强>
JAXB(JSR-222)实现为复杂类型生成Java类,因此您将为Discounts
复杂类型生成一个类。
package forum11981855;
import java.util.ArrayList;
import java.util.List;
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 = "Discounts", propOrder = {
"discounttest"
})
public class Discounts
extends ListWithSizeAttribute
{
@XmlElement(name = "Discounttest")
protected List<Discounttest> discounttest;
public List<Discounttest> getDiscounttest() {
if (discounttest == null) {
discounttest = new ArrayList<Discounttest>();
}
return this.discounttest;
}
}
<强>的ObjectFactory 强>
将在与全局元素@XmlElementDecl
对应的ObjectFactory
类上生成Discounts
注释。
package forum11981855;
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 _Discounts_QNAME = new QName("", "Discounts");
public ObjectFactory() {
}
public Discounttest createDiscounttest() {
return new Discounttest();
}
public Discounts createDiscounts() {
return new Discounts();
}
public ListWithSizeAttribute createListWithSizeAttribute() {
return new ListWithSizeAttribute();
}
@XmlElementDecl(namespace = "", name = "Discounts")
public JAXBElement<Discounts> createDiscounts(Discounts value) {
return new JAXBElement<Discounts>(_Discounts_QNAME, Discounts.class, null, value);
}
}
了解更多信息