我正在嘗試讀取XML文檔並將其解碼爲Java Bean。我的閱讀部分已經解決,但我遇到了一個問題。我基本上試圖解碼XML文檔的所有子節點,root是「目錄」。我如何使用XMLDecoder來做到這一點?JAXB讀取XML文檔
的XMLDecoder:
private static Book jaxbXMLToObject() {
try {
JAXBContext context = JAXBContext.newInstance(Book.class);
Unmarshaller un = context.createUnmarshaller();
Book book = (Book) un.unmarshal(new File("PATH"));
return book;
} catch (JAXBException e) {
e.printStackTrace();
}
return null;
}
我想閱讀下列文件
<?xml version="1.0"?>
<catalog>
<book id="1">
<author>Isaac Asimov</author>
<title>Foundation</title>
<genre>Science Ficition</genre>
<price>164</price>
<publish_date>1951-08-21</publish_date>
<description>Foundation is the first novel in Isaac Asimovs Foundation Trilogy (later expanded into The Foundation Series). Foundation is a cycle of five interrelated short stories, first published as a single book by Gnome Press in 1951. Collectively they tell the story of the Foundation, an institute to preserve the best of galactic civilization after the collapse of the Galactic Empire.</description>
</book>
</catalog>
而在一本書對象
@XmlRootElement(name = "book")
@XmlType(propOrder = {"id", "price", "title", "author", "genre", "description"})
public class Book {
private int id;
private int price;
private String title;
private String author;
private String genre;
private String description;
private Date publish_date;
public Book() {
}
解析它...... 我收到錯誤:jjavax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"catalog"). Expected elements are <{}book>
如何僅使用JAXB訪問子節點?
UPDATE
目錄類:
@XmlRootElement(name = "catalog")
public class Catalog {
@XmlElement(name = "book")
List<Book> books;
public List<Book> getBooks() {
return books;
}
public void setBooks(List<Book> books) {
this.books = books;
}
}
Book類:
@XmlAccessorType(XmlAccessType.FIELD)
public class Book {
@XmlAttribute
int id;
private int price;
private String title;
private String author;
private String genre;
private String description;
private Date publish_date;
public Book() {
}
public Book(int id, int price, String title, String genre, String description, Date publicationDate) {
this.id = id;
this.price = price;
this.title = title;
this.genre = genre;
this.description = description;
this.publish_date = publicationDate;
}
public int getId() {
return id;
}
public int getPrice() {
return price;
}
public String getTitle() {
return title;
}
public String getGenre() {
return genre;
}
public String getDescription() {
return description;
}
public Date getPublicationDate() {
return publish_date;
}
public void setId(int id) {
this.id = id;
}
public void setPrice(int price) {
this.price = price;
}
public void setTitle(String title) {
this.title = title;
}
public void setGenre(String genre) {
this.genre = genre;
}
public void setDescription(String description) {
this.description = description;
}
public void setPublish_date(String publish_date) {
this.publish_date = new Date();
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Date getPublish_date() {
return publish_date;
}
public String toJSON() {
ObjectMapper mapper = new ObjectMapper();
try {
return mapper.writeValueAsString(this);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
public String toString() {
return "Book{" +
"id=" + id +
", price=" + price +
", title='" + title + '\'' +
", genre='" + genre + '\'' +
", description='" + description + '\'' +
", publicationDate=" + publish_date +
'}';
}
}
DAO:
public class BooksDAO {
public BooksDAO() {
}
public List<Book> getBooks() {
Catalog catalog = jaxbXMLToObject();
return catalog.getBooks();
}
private static Catalog jaxbXMLToObject() {
try {
return JAXB.unmarshal(new File("PATH"), Catalog.class);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
傑克遜是否可以選擇使用Jackson的XML數據格式擴展來讀取XML?或者也許JAXB?使用XMLDecoder根本無法在這裏工作,因爲它被設計爲用作XMLEncoder的對應部分,該處理器爲特定的Bean序列化/反序列化處理特定的XML。 –
JAXB也是可能的,任何線索我會如何去這樣做呢?我現在試圖用JAXB來做,但當我試圖取書時,仍然會遇到這個愚蠢的目錄問題。 – Lithicas
那麼,你的XML文檔的根元素不是一個Bok。這是一本目錄。目錄中有一本書(或者可能是幾本書,更爲真實的例子)。因此創建一個Catalog類,其中包含一個List。將文檔解組到一個目錄(而不是一本書)。並從正在返回的Catalog對象中獲取書籍。 TL; DR:確保Java Bean結構與XML結構匹配。 –