简体   繁体   中英

XSD schema for xml file that can change for JAXB

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

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

}

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 eg

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM