XSD 具有 2 个根元素(一次 1 个) [英] XSD for having 2 root elements (1 at a time)

查看:25
本文介绍了XSD 具有 2 个根元素(一次 1 个)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以这是一个复杂/迟缓的情况.我正在编写一个 XSD,碰巧有一个要求,我需要 2 个根元素(在任何给定时间都需要 1 个)

So here is a complex/retarded situation. I am writing an XSD and there happens to be a requirement where i need 2 root elements (1 at any given time)

<xs:element name="booksList">
    <xs:complexType>
         <xs:sequence>
             <xs:element name="book" type="bookType" minOccurs="0" maxOccurs="unbounded"/>
         </xs:sequence> 
    </xs:complexType>   
</xs:element>

然后

<xs:element name="book" type="bookType"></xs:element>

在任何给定时间,这些元素中的任何一个都将用作根元素,因此 XML 看起来像

at any given time, either of these element will be used as the root element, so an XML would look like

<bookList>
<book>
<author>XYZ</author>
</book>
</bookList>

<book>
<author>XYZ</author>
</book>

这两个 XML 都将从 2 个不同的 URL 发送回用户,即列表将从 localhost/books.xml?author=XYZ 发送,单本书将从 发送>localhost/book_name.xml

Both of these XML will be sent back to the user from 2 different URL's i.e. the list will be sent from localhost/books.xml?author=XYZ and single book will be sent from localhost/book_name.xml

我怎样才能用一个 xml 实现这一目标?我尝试将书籍定义放在 XSD 中,但 JAXB2.1 没有生成任何 Book 类.有什么我遗漏的吗?

How can i achieve this with one xml ? I tried putting the book definition in the XSD but JAXB2.1 didn't generate any Book class. Is there something which i am missing ?

EDIT1:已生成 BookType,但 BookType 没有任何根元素.

EDIT1: BookType has been generated but BookType doesn't have any root element.

推荐答案

XML SCHEMA

我正在编写一个 XSD 并且恰好在我需要的地方有一个要求2 个根元素(在任何给定时间 1 个)

I am writing an XSD and there happens to be a requirement where i need 2 root elements (1 at any given time)

下面的 XML 模式支持您正在寻找的两个根元素 booksListbook.

The XML schema below supports having the two root elements booksList and book that you are looking for.

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

    <xs:element name="booksList">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="book" type="bookType" minOccurs="0"
                    maxOccurs="unbounded" />
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:element name="book" type="bookType"></xs:element>

    <xs:complexType name="bookType">
        <xs:sequence>
            <xs:element name="author" type="xs:string" />
        </xs:sequence>
    </xs:complexType>

</xs:schema>


生成模型

我尝试将书籍定义放在 XSD 中,但 JAXB2.1 没有生成任何 Book 类.

I tried putting the book definition in the XSD but JAXB2.1 didn't generate any Book class.

您的 JAXB (JSR-222) 实现将为命名的复杂类型 bookType 生成一个类,然后为 bookElement 创建一个 @XmlElementDecl<ObjectFactory 类上的/code> 注释.

Your JAXB (JSR-222) implementation will generate a class for the named complex type bookType, then for the bookElement it will create an @XmlElementDecl annotation on the ObjectFactory class.

图书列表

在这个类上生成了一个带有 @XmlRootElement 的类,因为它对应于一个具有匿名复杂类型的全局元素.

A class with an @XmlRootElement was generated on this class because it corresponds to a global element with an anonymous complex type.

package forum11620825;

import java.util.*;
import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {"book"})
@XmlRootElement(name = "booksList")
public class BooksList {

    protected List<BookType> book;

    public List<BookType> getBook() {
        if (book == null) {
            book = new ArrayList<BookType>();
        }
        return this.book;
    }

}

BookType

生成这个类是为了对应命名的复杂类型.

This class was generated to correspond to the named complex types.

package forum11620825;

import javax.xml.bind.annotation.*;

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "bookType", propOrder = {"author"})
public class BookType {

    @XmlElement(required = true)
    protected String author;

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String value) {
        this.author = value;
    }

}

对象工厂

与命名复杂类型对应的全局元素具有在 ObjectFactory 类上生成的 @XmlElementDecl 注释.这是必要的,因为多个全局元素可能对应于命名的复杂类型.

Global elements that correspond to named complex types have @XmlElementDecl annotations generated on the ObjectFactory class. This is necessary since multiple global elements could correspond to named complex types.

package forum11620825;

import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlElementDecl;
import javax.xml.bind.annotation.XmlRegistry;
import javax.xml.namespace.QName;

@XmlRegistry
public class ObjectFactory {

    private final static QName _Book_QNAME = new QName("", "book");

    public ObjectFactory() {
    }

    public BooksList createBooksList() {
        return new BooksList();
    }

    public BookType createBookType() {
        return new BookType();
    }

    @XmlElementDecl(namespace = "", name = "book")
    public JAXBElement<BookType> createBook(BookType value) {
        return new JAXBElement<BookType>(_Book_QNAME, BookType.class, null, value);
    }

}


XML

以下是您问题中的 XML 文档.

Below are the XML documents from your question.

booksList.xml

<booksList>
    <book>
        <author>XYZ</author>
    </book>
</booksList>

book.xml

<book>
    <author>XYZ</author>
</book>


演示代码

当您解组根元素对应于 @XmlRootElement 注释的文档时,您将获得相应域对象的实例.如果您解组根元素对应于 @XmlElementDecl 注释的文档,您将返回一个 JAXBElement 实例,该实例包装了与命名复杂类型对应的域对象.

When you unmarshal a document in which the root element corresponds to an @XmlRootElement annotation you get an instance of the corresponding domain object. If you unmarshal a document in which the root element corresponds to an @XmlElementDecl annotation you get back an instance of JAXBElement that wraps a domain object corresponding to the named complex type.

package forum11620825;

import java.io.File;
import javax.xml.bind.*;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance("forum11620825");
        Unmarshaller unmarshaller = jc.createUnmarshaller();

        File input1 = new File("src/forum11620825/booksList.xml");
        BooksList bookList = (BooksList) unmarshaller.unmarshal(input1);

        File input2 = new File("src/forum11620825/book.xml");
        JAXBElement<BookType> je = (JAXBElement<BookType>) unmarshaller.unmarshal(input2);
        BookType bookType = je.getValue();
    }

}


更新

下面的代码片段演示了如何将 BookType 的实例包装在 JAXBElement 中,以便对其进行编组.

Below is a code fragment demonstrating how to wrap an instance of BookType in a JAXBElement so that it can be marshalled.

ObjectFactory objectFactory = new ObjectFactory();
JAXBElement<BookType> jaxbElement = objectFactory.createBook(aBookType);
marshaller.marshal(jaxbElement, System.out);

这篇关于XSD 具有 2 个根元素(一次 1 个)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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