jaxb element with complexContent, restriction and attribute
I need to implement JAXB for the following XML model
<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>
I'm getting the following errors:
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.
I tried the following ways,
* <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>
I need to implement JAXB for the following XML model
OPTION #1 - Starting from Java Classes
You could start from Java objects and do the following:
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;
}
OPTION #2 - Starting from XML Schema
You don't need to generate your model from an XML schema. But if you wish to the XML schema could look something like the following:
<?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>
I generated this XML schema from the above annotated model using the following code:
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/51710.html
上一篇: 用java解析这个xml文件