为了账号安全,请及时绑定邮箱和手机立即绑定

代码在下面,头炸了

代码在下面,头炸了

冉冉说 2018-12-06 17:44:08
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>   现在的问题是它报错,我刚学也看不懂错误,搞了半天也没搞清楚是什么问题,求解决,代码完全附上也不多,多帮帮俺吧
查看完整描述

3 回答

?
眼眸繁星

TA贡献1873条经验 获得超9个赞

哼,报错页面都不给

查看完整回答
反对 回复 2018-12-16
?
阿晨1998

TA贡献2037条经验 获得超6个赞

@XmlRootElement(name="bookstore ") 多了个空格

查看完整回答
反对 回复 2018-12-16
?
慕娘9325324

TA贡献1783条经验 获得超5个赞

1.类Book的注解 @XmlRootElement(name="book") 的root根节点应该是 @XmlRootElement(name="bookstore"),如上所说。

2.另外类ReadBook中 Bookstore b=(Bookstore)u.unmarshal(new File("D:"+File.separator+"3.xml")); 调试下路径,然后直接在我的电脑中打开看看,是否存在(问题异常:handleEvent(Unknown Source) 。

 注:慢慢逐步排查,不要着急,可以百度下 。

查看完整回答
反对 回复 2018-12-16
  • 3 回答
  • 0 关注
  • 573 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号