所以这是一个复杂/迟钝的情况。我正在写一个 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 没有生成任何 Book 类。有什么我想念的吗?
EDIT1:已生成 BookType,但 BookType 没有任何根元素。
最佳答案
XML 模式
I am writing an XSD and there happens to be a requirement where i need 2 root elements (1 at any given time)
下面的 XML 架构支持您要查找的两个根元素 booksList
和 book
。
<?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>
生成的模型
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
类上的注释。
图书列表
在此类上生成了一个带有 @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);
关于java - 具有 2 个根元素的 XSD(一次 1 个),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11620825/