如何修复:javax.xml.bind.UnmarshalException:意外元素(uri:“”,本地:“目录”)。预期元素是(无)
Posted
技术标签:
【中文标题】如何修复:javax.xml.bind.UnmarshalException:意外元素(uri:“”,本地:“目录”)。预期元素是(无)【英文标题】:How to fix: javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"catalog"). Expected elements are (none) 【发布时间】:2021-08-01 12:52:38 【问题描述】:我正在尝试创建一个允许用户浏览和借书的列表视图,因此作为数据源,我想将 XML 加载并映射到内存中处理的对象中。在spring boot中,我对java和spring boot很陌生,我收到了这个错误:
**javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"catalog"). Expected elements are (none)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:712)
at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:232)
at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:227)
at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:94)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext$DefaultRootLoader.childElement(UnmarshallingContext.java:1117)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:542)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:524)
at com.sun.xml.bind.v2.runtime.unmarshaller.SAXConnector.startElement(SAXConnector.java:137)
at java.xml/com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.startElement(AbstractSAXParser.java:509)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(XMLNSDocumentScannerImpl.java:374)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl$NSContentDriver.scanRootElementHook(XMLNSDocumentScannerImpl.java:613)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:3058)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl$PrologDriver.next(XMLDocumentScannerImpl.java:820)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:601)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:112)
at java.xml/com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:531)
at java.xml/com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:885)
at java.xml/com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:821)
at java.xml/com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
at java.xml/com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)
at java.xml/com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:639)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:228)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:199)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:140)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:179)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:164)
at com.task1.task1.Task1Application.main(Task1Application.java:25)
**
这是我的 Book.java 类:
public class Book
private String author;
private String price;
private String genre;
private String description;
private String id;
private String title;
private String publish_date;
public Book()
public Book(String author, String price, String genre, String description, String id, String title, String publish_date)
super();
this.author = author;
this.price = price;
this.genre = genre;
this.description = description;
this.id = id;
this.title = title;
this.publish_date = publish_date;
public String getAuthor ()
return author;
public void setAuthor (String author)
this.author = author;
public String getPrice ()
return price;
public void setPrice (String price)
this.price = price;
public String getGenre ()
return genre;
public void setGenre (String genre)
this.genre = genre;
public String getDescription ()
return description;
public void setDescription (String description)
this.description = description;
public String getId ()
return id;
public void setId (String id)
this.id = id;
public String getTitle ()
return title;
@XmlElement(name = "title")
public void setTitle (String title)
this.title = title;
public String getPublish_date ()
return publish_date;
public void setPublish_date (String publish_date)
this.publish_date = publish_date;
这是我的 Catalog.java 类:
public class Catalog
private List<Book> books;
public Catalog()
public Catalog(List<Book> books)
super();
this.books = books;
@XmlElement
public List<Book> getBooks()
return books;
public void setBooks(List<Book> books)
this.books = books;
这是我的主文件:
public static void main(String[] args) throws JAXBException, IOException
try
File file = new File("books.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Catalog.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Catalog que= (Catalog) jaxbUnmarshaller.unmarshal(file);
System.out.println("Books:");
List<Book> list=que.getBooks();
for(Book bk:list)
System.out.println(bk.getId()+" "+bk.getAuthor()+" "+bk.getTitle());
catch (JAXBException e)
e.printStackTrace();
我的 books.xml 文件在这里: https://pastebin.com/C8udk7Mj
请让我知道如何解决此问题以及如何继续使用本地主机并允许阅读书籍...
【问题讨论】:
您需要在 Catalog 类中使用@XmlRootElement(name = "catalog")
来表明这可以是您文档的根标签,不是吗?
【参考方案1】:
我已经检查并可以确认。你需要
在您的 Catalog 类中添加@XmlRootElement(name = "catalog")
以告诉 JAXB 这可以在根目录中
在getBooks()方法上将注解改为@XmlElement(name="book")
(否则不匹配,找书)
在 Book setId
方法上添加注释 @XmlAttribute
(否则您最终会得到空 ID)
然后你的代码就可以工作了:
Books:
bk101 Gambardella, Matthew XML Developer's Guide
bk102 Ralls, Kim Midnight Rain
...
【讨论】:
以上是关于如何修复:javax.xml.bind.UnmarshalException:意外元素(uri:“”,本地:“目录”)。预期元素是(无)的主要内容,如果未能解决你的问题,请参考以下文章