XSD schema for xml file that can change for JAXB

2019-07-11 13:10发布

Hi I have just started learning about xsd as I want to use it with JAXB.

I was wondering I may have an XML tag that started with the same element but could have completely different child elements every time.

Here is an example1:

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

example2:

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

Basically how do I write this in xsd that "request" being asked can have completely different elements

Or if it is applicable is their a better way for me to process this xml then JAXB?

Thanks

标签: java xml xsd jaxb
2条回答
混吃等死
2楼-- · 2019-07-11 13:17

The whole point of XSD is to constrain the XML message so that it is easier to process by the applications. I suggest you rethink the approach to have a separate element for each type of request. For e.g.

<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>
查看更多
聊天终结者
3楼-- · 2019-07-11 13:41

You can define your request element as:

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

This will cause JAXB to generate a field/property that is annotated like:

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

FULL EXAMPLE

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 Call

xjc -d out -p forum8776746 any.xsd

Service

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

}

Request

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

}
查看更多
登录 后发表回答