带有complexContent,restriction和attribute的jaxb元素

我需要为以下XML模型实现JAXB

<OrderItemList>
    <ItemCount>2</ItemCount>
    <OrderItem Sequence="1">
      <ItemId>12345</ItemId>
      <Quantity>1</Quantity>
      <DiscountId>prm</DiscountId>
      <DiscountPercent>3.5</DiscountPercent>
    </OrderItem>
    <OrderItem Sequence="1">
      <ItemId>44523</ItemId>
      <Quantity>3</Quantity>
      <DiscountId>nil</DiscountId>
      <DiscountPercent>0</DiscountPercent>
    </OrderItem>
</OrderItemList>

我收到以下错误:

16:15:28,564 WARN  [OrderItemValidate] [error] null : cvc-complex-type.3.2.2: Attribute 'Sequence' is not allowed to appear in element 'OrderItem'.
16:15:28,565 ERROR [OrderItemValidate] org.xml.sax.SAXParseException; lineNumber: 7; columnNumber: 24; cvc-complex-type.3.2.2: Attribute 'Sequence' is not allowed to appear in element 'OrderItem'.
org.xml.sax.SAXParseException; lineNumber: 7; columnNumber: 24; cvc-complex-type.3.2.2: Attribute 'Sequence' is not allowed to appear in element 'OrderItem'.

22:05:09,966 WARN  [OrderItemValidate] [error] null : cvc-complex-type.2.4.a: Invalid content was found starting with element 'TotalCount'. One of '{"www.myapp.com":OrderItem}' is expected.
22:05:09,967 ERROR [OrderItemValidate] org.xml.sax.SAXParseException; lineNumber: 7; columnNumber: 17; cvc-complex-type.2.4.a: Invalid content was found starting with element 'TotalCount'. One of '{"www.myapp.com":OrderItem}' is expected.
org.xml.sax.SAXParseException; lineNumber: 7; columnNumber: 17; cvc-complex-type.2.4.a: Invalid content was found starting with element 'TotalCount'. One of '{"www.myapp.com":OrderItem}' is expected.

我尝试了以下方法,

 * <complexType name="OrderItem">
 *   <complexContent>
 *     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       <all>
 *         <attribute name="Sequence" type="{www.myapp.com}Integer" use="required"/>
 *         <element name="ItemId" type="{www.myapp.com}String" minOccurs="0"/>
 *         <element name="Quantity" type="{www.myapp.com}Integer" minOccurs="0"/>
 *         <element name="DiscountId" type="{www.myapp.com}Number" minOccurs="0"/>
 *         <element name="DiscountPercent" type="{www.myapp.com}Number" minOccurs="0"/>
 *       </all>
 *     </restriction>
 *   </complexContent>
 * </complexType>
 * 
  •   <ItemId>12345</ItemId>
      <Quantity>1</Quantity>
      <DiscountId>prm</DiscountId>
      <DiscountPercent>3.5</DiscountPercent>
    

  • 我需要为以下XML模型实现JAXB


    选项1 - 从Java类开始

    您可以从Java对象开始并执行以下操作:

    OrderItemList

    import java.util.*;
    import javax.xml.bind.annotation.*;
    
    @XmlRootElement(name="OrderItemList")
    @XmlType(propOrder={"itemCount", "orderItems"})
    @XmlAccessorType(XmlAccessType.FIELD)
    public class OrderItemList {
    
        @XmlElement(name="OrderItem")
        private List<OrderItem> orderItems = new ArrayList<OrderItem>();
    
        @XmlElement(name="ItemCount")
        public int getItemCount() {
            return orderItems.size();
        }
    
    }
    

    OrderItem的

    import javax.xml.bind.annotation.*;
    
    @XmlAccessorType(XmlAccessType.FIELD)
    public class OrderItem {
    
        @XmlAttribute(name="Sequence")
        private int sequence;
    
        @XmlElement(name="ItemId")
        private int itemId;
    
        @XmlElement(name="Quantity")
        private int quantity;
    
        @XmlElement(name="DiscountId")
        private String discountId;
    
        @XmlElement(name="DiscountPercent")
        private float discountPercent;
    
    }
    

    选项2 - 从XML模式开始

    您不需要从XML模式生成模型。 但是如果你希望XML模式看起来如下所示:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    
      <xs:element name="OrderItemList" type="orderItemList"/>
    
      <xs:complexType name="orderItemList">
        <xs:sequence>
          <xs:element name="ItemCount" type="xs:int"/>
          <xs:element name="OrderItem" type="orderItem" minOccurs="0" maxOccurs="unbounded"/>
        </xs:sequence>
      </xs:complexType>
    
      <xs:complexType name="orderItem">
        <xs:sequence>
          <xs:element name="ItemId" type="xs:int"/>
          <xs:element name="Quantity" type="xs:int"/>
          <xs:element name="DiscountId" type="xs:string" minOccurs="0"/>
          <xs:element name="DiscountPercent" type="xs:float"/>
        </xs:sequence>
        <xs:attribute name="Sequence" type="xs:int" use="required"/>
      </xs:complexType>
    
    </xs:schema>
    

    我使用下面的代码从上面注释的模型中生成了这个XML模式:

    import java.io.IOException;
    import javax.xml.bind.*;
    import javax.xml.transform.Result;
    import javax.xml.transform.stream.StreamResult;
    
    public class GenerateSchema {
    
        public static void main(String[] args) throws Exception {
            JAXBContext jc = JAXBContext.newInstance(OrderItemList.class);
            jc.generateSchema(new SchemaOutputResolver() {
    
                @Override
                public Result createOutput(String namespaceUri, String suggestedFileName)
                        throws IOException {
                    StreamResult result = new StreamResult(System.out);
                    result.setSystemId(suggestedFileName);
                    return result;
                }
            });
        }
    
    }
    
    链接地址: http://www.djcxy.com/p/51709.html

    上一篇: jaxb element with complexContent, restriction and attribute

    下一篇: What is the motivation for bringing Symbols to ES6?