可以为JAXB更改的xml文件的XSD架构 [英] XSD schema for xml file that can change for JAXB

查看:66
本文介绍了可以为JAXB更改的xml文件的XSD架构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我刚开始学习xsd,因为我想将它与JAXB一起使用。

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

我想知道我可能有一个以相同元素开头的XML标记但每次都可能有完全不同的子元素。

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

这是一个例子1:

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

基本上我如何在xsd中写这个请求被要求可以有完全不同的元素

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

或者如果它适用于他们更好的方式来处理这个xml然后JAXB?

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

谢谢

推荐答案

您可以将请求元素定义为:

<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生成一个注释如下的字段/属性:

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

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

}

这篇关于可以为JAXB更改的xml文件的XSD架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆