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>
现在的问题是它报错,我刚学也看不懂错误,搞了半天也没搞清楚是什么问题,求解决,代码完全附上也不多,多帮帮俺吧
冉冉说
眼眸繁星
阿晨1998
慕娘9325324
随时随地看视频慕课网APP
相关分类