package per.sww.four.foursenver;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="book")
public class Book {
public Book(){
super();
}
private String category;
private Title title;
private String author;
private String year;
private String price;
@XmlAttribute(name="category")
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
@XmlElement(name="title")
public Title getTitle() {
return title;
}
public void setTitle(Title title) {
this.title = title;
}
@XmlElement(name="author")
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
@XmlElement(name="year")
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
@XmlElement(name="price")
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
}
package per.sww.four.foursenver;
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
public class ReadBook {
public static void main(String[] args){
try{
JAXBContext jc=JAXBContext.newInstance(Bookstore.class);
Unmarshaller u=jc.createUnmarshaller();
Bookstore b=(Bookstore)u.unmarshal(new File("D:"+File.separator+"3.xml"));
for(Book book:b.getBooks()){
for(Title title:b.getTitles()){
System.out.println(title.getLang());
}
System.out.println(book.getTitle());
System.out.println(book.getAuthor());
System.out.println(book.getYear());
System.out.println(book.getPrice());
}
}catch(JAXBException e){
e.printStackTrace();
}
}
}
package per.sww.four.foursenver;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="title ")
public class Title {
public Title(){
super();
}
private String lang;
@XmlAttribute(name="lang")
public String getLang() {
return lang;
}
public void setLang(String lang) {
this.lang = lang;
}
}
package per.sww.four.foursenver;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElements;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="bookstore ")
public class Bookstore {
public Bookstore(){
super();
}
List<Book> books;
@XmlElements(value={@XmlElement(name="book",type=Book.class)})
public List<Book> getBooks() {
return books;
}
public void setBooks(List<Book> books) {
this.books = books;
}
List<Title> titles;
@XmlElements(value={@XmlElement(name="title",type=Title.class)})
public List<Title> getTitles() {
return titles;
}
public void setTitles(List<Title> titles) {
this.titles = titles;
}
}
?
<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book category="children">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="cooking">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="web">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
<book category="web">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>
</bookstore>
?
現(xiàn)在的問題是它報(bào)錯(cuò),我剛學(xué)也看不懂錯(cuò)誤,搞了半天也沒搞清楚是什么問題,求解決,代碼完全附上也不多,多幫幫俺吧
3 回答

慕娘9325324
TA貢獻(xiàn)1783條經(jīng)驗(yàn) 獲得超4個(gè)贊
1.類Book的注解 @XmlRootElement(name="book") 的root根節(jié)點(diǎn)應(yīng)該是 @XmlRootElement(name="bookstore"),如上所說。
2.另外類ReadBook中 Bookstore b=(Bookstore)u.unmarshal(new File("D:"+File.separator+"3.xml")); 調(diào)試下路徑,然后直接在我的電腦中打開看看,是否存在(問題異常:handleEvent(Unknown Source) 。
?注:慢慢逐步排查,不要著急,可以百度下 。
添加回答
舉報(bào)
0/150
提交
取消