首页 新闻 搜索 专区 学院

代码在下面,头炸了

0
悬赏园豆:30 [已解决问题] 解决于 2016-04-08 13:47
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>

 

现在的问题是它报错,我刚学也看不懂错误,搞了半天也没搞清楚是什么问题,求解决,代码完全附上也不多,多帮帮俺吧

问题补充:
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)
师太来老道这坐坐的主页 师太来老道这坐坐 | 初学一级 | 园豆:34
提问于:2016-04-07 17:02
< >
分享
最佳答案
0

哼,报错页面都不给

小光 | 小虾三级 |园豆:1830 | 2016-04-07 17:05

给给给,哪能不给,我现在在炸了

师太来老道这坐坐 | 园豆:34 (初学一级) | 2016-04-07 17:07

亲哥啊,帮我整整,我看的都不会看了

师太来老道这坐坐 | 园豆:34 (初学一级) | 2016-04-07 17:10

@略知闻风雨: 

1.你的Bookstore类的注解中@XmlRootElement(name="bookstore ")bookstore后面多一个空格,把空格删掉。

2.完成1后会出现新的问题,ReadBook第14行:   for(Title title:b.getTitles()){    会出现空指针异常,是由于你在Bookstore中把book和title设为同级元素,而你在xml中写的是title是book的子元素。

 

刚才有点事情耽搁了,没给你看,第二个问题是你解决第一个问题后出现的,跟你提的问题没关系

小光 | 园豆:1830 (小虾三级) | 2016-04-07 18:27
其他回答(2)
0

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

阿克西斯教成员污米饭 | 园豆:129 (初学一级) | 2016-04-07 18:27
0

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) 。

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

收获园豆:30
rickbao | 园豆:267 (菜鸟二级) | 2016-04-07 23:29
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册