绑定元素,他是我的XML托管远程机器上:JAXB:如何命名空间
<?xml version="1.0" encoding="UTF-8"?>
<store xmlns="http://mydomain.com/store/schemas">
<!-- My Book store-->
<book location="vk 1">
<title>Learning JAXB</title>
<author>Joe Blogg</author>
</book>
<book location="vk 1">
<title>Learning JAXB SE</title>
<author>Joe Blogg</author>
</book>
</store>
我有绑定的书如下:
书籍装帧:
@XmlRootElement(name = "book")
@XmlType(propOrder = { "title", "author"})
public class Book
{
private String title;
private String author;
private String location;
@XmlElement(name = "title")
public String getTitle()
{
return title;
}
public void setTitle(String title)
{
this.title=title;
}
@XmlAttribute(name = "location")
public String getLocation()
{
return location;
}
public void setLocation(String location)
{
this.location = location;
}
@XmlElement(name = "author")
public String getAuthor()
{
return author;
}
public void setAuthor(String author)
{
this.author = author;
}
}
商店装订:
@XmlRootElement(name = "store", namespace = "http://mydomain.com/store/schemas")
public class Store
{
private List<Book> books;
@XmlElement(name="book")
public List<Book> getBooks()
{
return books;
}
public void setBooks(List<Book> books)
{
this.books= books;
}
}
我解编XML
文件fAollows:
JAXBContext context = JAXBContext.newInstance(Store.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
URL url = new URL("http://mydomain/files/store.xml");
Store s= (Store) unmarshaller.unmarshal(url);
System.out.println(s.getBooks());// Prints null
当我打电话getBooks()
我们得到空值。任何人都可以发现我在这里做错了吗?!
+1。当为绑定文件中的模式提供包名时,XJC将使用此机制。至少我总是看到它在JAXB 2.1 RI以上的条件下在'package-info.java'上使用'@ XmlSchema'。 –