2 qq 15173955 qq_15173955 于 2016.04.07 15:45 提问

java用注解实现将xml中的值获取到并在控制台打印为什么出错

<?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)
此代码返回的错误信息


如上所示,我看了好几遍没找到我的逻辑错误啊,请各位大神指点一下,名称大小写也检查过了,郁闷

3个回答

qq_15173955
qq_15173955   2016.04.07 15:54

调试了下,发现程序运行到for循环那里就直接跳过了
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了

xionglangs
xionglangs   Rxr 2016.04.07 16:50

这里解析报错,断点看看b返回的是什么,看看b.getBtListl()返回的是什么。

jituoxiu8900
jituoxiu8900 为什么运行u.unmarshal(new File(file))就直接进入xmlconfiguration.java代码报错了。。。这个最后怎么解决的
3 个月之前 回复
xionglangs
xionglangs 如果是这样的话就是现在的xml不能对应实体类,去网上看看Unmarshaller的对应规则
一年多之前 回复
qq_15173955
qq_15173955 回复编程爱好者熊浪: 我前面一个示例比这个简单,也是这么做的没错,这个示例子节点多了个属性就出错了
一年多之前 回复
xionglangs
xionglangs 那就是这句代码就已经出错了。拆分这段代码,看看具体是那个方法出错了。
一年多之前 回复
qq_15173955
qq_15173955 回复编程爱好者熊浪: 看不到的,调试的时候B没出来
一年多之前 回复
xionglangs
xionglangs 断点在这行的时候Bookstore b=(Bookstore)u.unmarshal(new File("D:"+File.separator+"3.xml"));可以看到b是什么吗
一年多之前 回复
qq_15173955
qq_15173955 回复编程爱好者熊浪: 运行到for循环上面也就是Bookstore b=(Bookstore)u.unmarshal(new File("D:"+File.separator+"3.xml"));这一行读完,直接跳入catch了,很头疼
一年多之前 回复
qq_15173955
qq_15173955 回复编程爱好者熊浪: for循环它直接跳过了都不进去
一年多之前 回复
xionglangs
xionglangs 在Book book:b.getBtListl()这里解析报错。
一年多之前 回复
qq_15173955
qq_15173955   2016.04.07 17:13

javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"bookstore"). Expected elements are <{}book>,<{}bookstore >,<{}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)

这是错误信息

Csdn user default icon
上传中...
上传图片
插入图片