【问题标题】:UnmarshalException while using JAXB使用 JAXB 时出现 UnmarshalException
【发布时间】:2014-12-18 07:33:00
【问题描述】:

我正在尝试运行此代码:

JAXBContext jaxbContext = JAXBContext.newInstance(ObjectFactory.class);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        Tags tags = (Tags) unmarshaller.unmarshal(new File("Tags.xml"));

但是,我得到了这个错误:

javax.xml.bind.UnmarshalException:意外元素(uri:“”,本地:“标签”)。预期的元素是(无)

如何防止发生此异常?这是我的根元素:

@XmlRootElement(name = "tags")

【问题讨论】:

  • 您是从 XML Schema 生成模型的吗?
  • 我对此很天真,我点击了这个链接:youtube.com/watch?v=4J_ytgQ96Kg 并做了它所拥有的一切

标签: xml jaxb unmarshalling


【解决方案1】:

JAXB 不知道应该处理哪些类。

    JAXBContext jaxbContext = JAXBContext.newInstance(Tags.class);
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    Tags tags = (Tags) unmarshaller.unmarshal(new File("Tags.xml"));

【讨论】:

    猜你喜欢
    • 2011-07-03
    • 1970-01-01
    • 2012-09-07
    • 1970-01-01
    • 2017-05-13
    • 2019-03-16
    • 1970-01-01
    • 2020-01-31
    • 2017-10-10
    相关资源
    最近更新 更多