<?xml version="1.0" encoding="ISO-8859-1" ?>
- Harry PotterJ K. Rowling200529.99
- Everyday ItalianGiada De Laurentiis200530.00
- Learning XMLErik T. Ray200339.95
-
XQuery Kick Start
James McGovern
Per Bothner
Kurt Cagle
James Linn
Vaidyanathan Nagarajan
2003
49.99这是需要获取的xml
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 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="book ")
public class Bookstore {
public Bookstore(){
super();
}
List<Book> btListl;
@XmlElements(value={@XmlElement(name="book",type=Book.class)})
public List<Book> getBtListl() {
return btListl;
}
public void setBtListl(List<Book> btListl) {
this.btListl = btListl;
}
}
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.getBtListl()){
System.out.println(book.getTitle().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();
}
}
}
javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"bookstore"). Expected elements are <{}book>,<{}book >,<{}title >
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportError(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.startElement(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(Unknown Source)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source)
at per.sww.four.foursenver.ReadBook.main(ReadBook.java:14)
此代码返回的错误信息
如上所示,我看了好几遍没找到我的逻辑错误啊,请各位大神指点一下,名称大小写也检查过了,郁闷