简体   繁体   English

JAXB Unmarshalling不起作用。预期要素是(无)

[英]JAXB Unmarshalling not working. Expected elements are (none)

I am trying to unmarshal an XML. 我正在尝试解组XML。

This is what my XML looks like 这就是我的XML的样子

<DeviceInventory2Response xmlns="http://tempuri.org/">
<DeviceInventory2Result xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
  <Obj123 xmlns="">
     <Id>1</Id>
     <Name>abc</Name>
  </Obj123>
  <Obj456 xmlns="">
  .
  .
  .

I am trying to get Id and Name under Obj123. 我想在Obj123下获得Id和Name。 However when I run my unmarshal command I get the following error. 但是,当我运行我的unmarshal命令时,我收到以下错误。

An Error:  javax.xml.bind.UnmarshalException: unexpected element (uri:"http://tempuri.org/", local:"DeviceInventory2Response"). Expected elements are (none)

My code looks like this in the main class: 我的代码在主类中看起来像这样:

Obj123 myObj123 = (Obj123) unmarshaller.unmarshal(inputSource);

And my class for Obj123 looks like this: 而我的Obj123课程看起来像这样:

package com.myProj.pkg;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;


@XmlRootElement(name="Obj123")
public class Obj123 {

  private String Id;
  private String Name;

  public String getId() {
    return Id;
  }

  public String getName() {
    return Name;
  }
}

I thought by setting my XMLRootElement that I should be able to skip the first 2 lines of my XML but that doesn't seem to be happening. 我想通过设置我的XMLRootElement我应该能够跳过我的XML的前两行,但这似乎并没有发生。 Any ideas? 有任何想法吗?

Edit: 编辑:

This is how my JAXB Context is made: 这就是我的JAXB上下文的制作方式:

JAXBContext jaxbContext = JAXBContext.newInstance();
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
Obj123 obj123 = (Obj123) unmarshaller.unmarshal(xmlStreamReader);

I solved the problem by adding 我通过添加解决了这个问题

@XmlRootElement(name="abc_xxx") to the Root class. @XmlRootElement(name="abc_xxx")到Root类。
(where abc_XXX is the root tag of your XML) (其中abc_XXX是XML的根标记)

The JAXB classes generated by eclipse didn't add this annotation to my root class. eclipse生成的JAXB类没有将此注释添加到我的根类。

JAXB implementations will try to match on the root element of the document (not on a child element). JAXB实现将尝试匹配文档的根元素(而不是子元素)。 If you want to unmarshal to the middle of an XML document then you can parse the document with StAX advance the XMLStreamReader to the desired element and then unmarshal that. 如果要解组到XML文档的中间,那么可以使用StAX解析文档,将XMLStreamReader推进到所需的元素,然后解组。

For More Information 欲获得更多信息

UPDATE UPDATE

now I am getting the following error. 现在我收到以下错误。 An Error: javax.xml.bind.UnmarshalException - with linked exception: [javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"Obj123"). 错误:javax.xml.bind.UnmarshalException - 包含链接异常:[javax.xml.bind.UnmarshalException:unexpected element(uri:“”,local:“Obj123”)。 Expected elements are (none)]. 预期的元素是(无)]。

A JAXBContext only knows about the classes you tell it about. JAXBContext只知道你告诉它的类。 Instead of: 代替:

JAXBContext jaxbContext = JAXBContext.newInstance();

You need to do: 你需要这样做:

JAXBContext jaxbContext = JAXBContext.newInstance(Obj123.class);

Use ObjectFactory class instead like 请改用ObjectFactory类

JAXBContext jaxbContext = null;
try {
    jaxbContext = JAXBContext.newInstance(ObjectFactory.class);
} catch (JAXBException e) {
    e.printStackTrace();
}

JAXBElement<ObjectFactory> applicationElement = null;
try {
    applicationElement = (JAXBElement<ObjectFactory>)
    unmarshaller.unmarshal(Thread.currentThread().getClass()
        .getResourceAsStream(fileName));
} catch (JAXBException e) {
    e.printStackTrace();
}

Try this and will resolve above problem. 试试这个并解决上述问题。 My problem has been resolved. 我的问题已经解决了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM