【问题标题】:XSD for having 2 root elements (1 at a time)XSD 具有 2 个根元素(一次 1 个)
【发布时间】:2012-07-23 21:30:35
【问题描述】:

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

<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 看起来像

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

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

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

如何使用一个 xml 来实现这一点?我尝试将书籍定义放在 XSD 中,但 JAXB2.1 没有生成任何书籍类。有什么我想念的吗?


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

【问题讨论】:

  • 它可能会生成一个BookType 类,并在必要时用@XMLElement 注释字段。请列出生成的类。你有BookType 课程吗?它有@XMLRootElement 注释吗?
  • 问题已更新为 BookType 响应。
  • @EmAe BookType 类是Book 的Java 表示。使用@XmlRootElement 对其进行注释,它应该可以正常工作。如有必要,您还可以使用 @XmlElement(name="foo") 调整元素名称。

标签: java xml xsd jaxb jaxb2


【解决方案1】:

XML 架构

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

下面的 XML 架构支持拥有您正在寻找的两个根元素 booksListbook

<?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 类。

您的 JAXB (JSR-222) 实现将为命名复杂类型 bookType 生成一个类,然后对于 bookElement,它将在 ObjectFactory 类上创建一个 @XmlElementDecl 注释。

书单

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

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

}

书型

生成此类以对应于命名的复杂类型。

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 注释。这是必要的,因为多个全局元素可能对应于命名的复杂类型。

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 文档。

booksList.xml

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

book.xml

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

演示代码

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

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 中,以便对其进行编组。

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

【讨论】:

  • 如何通过这个例子从 Object 转换为 XML?我看到 bookType 有XmlElementDecl。但是当我尝试从 BookType 创建一个对象并将其编组为 XML 时,我得到了一个异常 com.sun.istack.SAXException2: unable to marshal type "jaxb.test.BookType" as an element because it is missing an @XmlRootElement annotation
  • @EmAe - 我在答案中添加了一个更新,以演示如何编组BookType 的实例。
  • 是否可以有一个 XSD,其中 2 个 RootElements 具有相同的 RootElementName "response" 但其中有 2 个不同的 complexType?
【解决方案2】:

看到这个previous question。您可以通过简单地按顺序列出可能性来对根元素执行 的等效操作,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="bookList">
        <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:schema>

【讨论】:

  • 这不正是他在问题中发布的内容(只是没有schema 元素)吗?
  • 我在根目录下有两个xs:elements。
  • 换句话说,任何有效的 XML 文档都只能有一个根元素。如果您在 XML 模式中定义多个根元素,这意味着这些元素中的任何一个都可以是根元素,但在单个 XML 文档中只能有一个(即,您从模式中选择一个根元素作为根元素)文件)
猜你喜欢
  • 1970-01-01
  • 2017-01-12
  • 1970-01-01
  • 1970-01-01
  • 2013-11-02
  • 1970-01-01
  • 2014-05-19
  • 1970-01-01
  • 2015-03-09
相关资源
最近更新 更多