2012-01-08 65 views
2

嗨,我剛開始學習xsd,因爲我想用它與JAXB。XSD架構的XML文件,可以更改爲JAXB

我想知道我可能有一個XML標籤,它以相同的元素開始,但每次都可能有完全不同的子元素。

下面是一個例1:

<service>Hotel 
     <request>location 
      <currentLongitude>100</currentLongitude> 
      <currentLatitude>100</currentLatitude> 
     </request> 
    </service> 

例2:

<service>Hotel 
     <request>Price 
      <Single>130</Single> 
      <Double>140</Double> 
     </request> 
    </service> 

基本上我怎麼寫這篇文章的XSD 「請求」 被問可有完全不同的元素

或者,如果它適用於他們更好的方式來處理這個XML然後JAXB?

感謝

回答

1

您可以定義request元素:

<xsd:element name="request"> 
    <xsd:complexType mixed="true"> 
     <xsd:sequence> 
      <xsd:any minOccurs="0" maxOccurs="unbounded" processContents="lax"/> 
     </xsd:sequence> 
    </xsd:complexType> 
</xsd:element> 

這將導致JAXB生成被註釋就像一個字段/屬性:

@XmlMixed 
@XmlAnyElement(lax = true) 
protected List<Object> content; 

完整的例子

any.xsd

<?xml version="1.0" encoding="UTF-8"?> 
<xsd:schema 
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 

    <xsd:element name="service"> 
     <xsd:complexType> 
      <xsd:sequence> 
       <xsd:element ref="request"/> 
      </xsd:sequence> 
     </xsd:complexType> 
    </xsd:element> 

    <xsd:element name="request"> 
     <xsd:complexType mixed="true"> 
      <xsd:sequence> 
       <xsd:any minOccurs="0" maxOccurs="unbounded" processContents="lax"/> 
      </xsd:sequence> 
     </xsd:complexType> 
    </xsd:element> 

</xsd:schema> 

XJC呼叫

xjc -d out -p forum8776746 any.xsd 

服務

package forum8776746; 

import javax.xml.bind.annotation.XmlAccessType; 
import javax.xml.bind.annotation.XmlAccessorType; 
import javax.xml.bind.annotation.XmlElement; 
import javax.xml.bind.annotation.XmlRootElement; 
import javax.xml.bind.annotation.XmlType; 

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "", propOrder = { 
    "request" 
}) 
@XmlRootElement(name = "service") 
public class Service { 

    @XmlElement(required = true) 
    protected Request request; 

    public Request getRequest() { 
     return request; 
    } 

    public void setRequest(Request value) { 
     this.request = value; 
    } 

} 

請求

package forum8776746; 

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.XmlAnyElement; 
import javax.xml.bind.annotation.XmlMixed; 
import javax.xml.bind.annotation.XmlRootElement; 
import javax.xml.bind.annotation.XmlType; 

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "", propOrder = { 
    "content" 
}) 
@XmlRootElement(name = "request") 
public class Request { 

    @XmlMixed 
    @XmlAnyElement(lax = true) 
    protected List<Object> content; 

    public List<Object> getContent() { 
     if (content == null) { 
      content = new ArrayList<Object>(); 
     } 
     return this.content; 
    } 

} 
0

XSD的全部意義在於約束XML消息,使得它更容易被應用程序處理。我建議你重新考慮對每種請求都有一個單獨元素的方法。對於例如

<service> 
     <hotel-location-request> 
      <currentLongitude>100</currentLongitude> 
      <currentLatitude>100</currentLatitude> 
     </hotel-location-request> 
</service> 

<service>Hotel 
     <hotel-price-request> 
      <Single>130</Single> 
      <Double>140</Double> 
     </hotel-price-request> 
</service>