0

我需要为以下 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">
 * <复杂内容>
 * <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 * <全部>
 * <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"/>
 * </全部>
 * </限制>
 * </复杂内容>
 * </complexType>
 *
  •   <ItemId>12345</ItemId>
      <Quantity>1</Quantity>
      <DiscountId>prm</DiscountId>
      <DiscountPercent>3.5</DiscountPercent>
    
4

1 回答 1

1

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


选项 #1 - 从 Java 类开始

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

订单项列表

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();
    }

}

订单项

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;
            }
        });
    }

}
于 2013-01-27T11:10:38.043 回答