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

问题描述

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

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

解决方案

调试了下发现程序运行到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了

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

解决方案三:
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)

这是错误信息

时间: 2024-12-20 21:40:15

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

安卓修改colors.xml中的值

问题描述 安卓修改colors.xml中的值 我在colors.xml中定义了一个main_bg参数,并赋了值#288df5#288df5 求问各位大神,有没有什么方法,能够修改这个参数的值? 比如说我点击某个按钮,能够将main_bg的值修改为#C1FFC1? 谢谢啊 <color name="main_bg">#288df5</color> 解决方案 你是想设计按钮点击与非点击状态下不同的颜色吧?! 如果是这样,你在drawable中写一个selector

字符串用xml输出-asp.net替换xml中的值并输出xml

问题描述 asp.net替换xml中的值并输出xml 例: xml 模板: <?xml version="1.0" standalone="yes"?> {Manpower.ID},{Manpower.Name} {Manpower.Age}{Manpower.Gender} {Project.ID} 替换掉所有{*} 问题: 先将xml模板读成string类型,找到所有的{*},但是不知道怎样将{*}替换成想要的值,并将替换值后字符串用xml输出,坐等

php中checkbox值获取,显示,多选值获取

php教程中checkbox值获取,显示,多选值获取 最简单checkbox获取值代码 <html> <head> <title>checkbox demo</title> </head> <body> <h1>checkbox demo</h1> <h3>demonstrates checkboxes</h3> <form action ="handleformchec

基于android中的各种颜色在drawable.xml中的值详解_Android

< drawable name="white">#FFFFFF< /drawable>< !--白色 --> < drawable name="black">#000000< /drawable>< !--黑色 --> < drawable name="ivory">#FFFFF0< /drawable>< !--象牙色 --> <

基于android中的各种颜色在drawable.xml中的值详解

< drawable name="white">#FFFFFF< /drawable>< !--白色 --> < drawable name="black">#000000< /drawable>< !--黑色 --> < drawable name="ivory">#FFFFF0< /drawable>< !--象牙色 --> <

java 没有setName()方法,如何更改name值

问题描述 java 没有setName()方法,如何更改name值 获取一个对像,但没有setName()方法,只有getName()如何更改name值 解决方案 在类中设置void setName(String name)这样在创建这个类的对象的时候调用且传入参数可以就可以修改这个值了. 解决方案二: 你可以考虑下构造方法传参

用java实现:在下面xml中添加一本书的信息,用dom4j实现

问题描述 用java实现:在下面xml中添加一本书的信息,用dom4j实现<?xmlversion="1.0"?><library><book><id>b001</id><title>我是一个兵</title><author>大兵</author></book></library> 解决方案 解决方案二:我刚好做过类似的:book.xml文件<?xm

java注解及在butternife中的实践和原理

1.  背景 之前去一个公司,说到了java的注解,问java的注解有几种方式,然后我提到了android中的butternife和afinal注解工具,我们知道butternife在6.1版本的时候还是InjectView,可以到7.1的时候用的却是BInd,这里面有什么区别吗.简单的说,InjectView是在运行的时间进行注解,而BInd在android安装的时候就执行了,肯定后者的执行效率更高些. 接下来说说java的注解吧. 2.  什么是注解 我们有必要对JDK 5.0新增的注解(A

java中有个图片链接,我想将链接放入xml中方便修改

问题描述 java中有个图片链接,我想将链接放入xml中方便修改 我是初学者,不是很懂xml 我现在做了个对图片链接(File fileOne = new File("E:1.jpg");)合成的java程序 我想将图片链接放到xml中来读取 我改如何配置xml,和对java代码修改 感谢! 解决方案 jdom xml 解决方案二: 新建xml文件放到本项目目录下,在网页中使用js通过ajax获取xml文件,然后进行解析,得到url字段后,利用js找到该节点,再将link属性更改为该u