Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JAXB解组不工作。预期元素为(无)_Java_Jaxb_Unmarshalling - Fatal编程技术网

Java JAXB解组不工作。预期元素为(无)

Java JAXB解组不工作。预期元素为(无),java,jaxb,unmarshalling,Java,Jaxb,Unmarshalling,我正在尝试解组一个XML 这就是我的XML的样子 <DeviceInventory2Response xmlns="http://tempuri.org/"> <DeviceInventory2Result xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> <Obj123 xmlns=""> <Id>1</Id> <Name>abc</Na

我正在尝试解组一个XML

这就是我的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="">
  .
  .
  .
在主类中,我的代码如下所示:

Obj123 myObj123 = (Obj123) unmarshaller.unmarshal(inputSource);
我的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;
  }
}
我认为通过设置XMLRootElement,我应该能够跳过XML的前两行,但这似乎没有发生。有什么想法吗

编辑:

以下是我的JAXB上下文的创建方式:

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

JAXB实现将尝试匹配文档的根元素(而不是子元素)。 如果要将XML文档的中间部分解组,则可以使用StAX将
XMLStreamReader
解析为所需元素,然后将其解组

了解更多信息

更新 现在我得到以下错误。错误: javax.xml.bind.UnmarshaleException-带有链接异常: [javax.xml.bind.UnmarshaleException:意外元素(uri:”, 本地:“Obj123”)。预期元素为(无)]

JAXBContext
只知道您告诉它的类。而不是:

JAXBContext jaxbContext = JAXBContext.newInstance();
您需要执行以下操作:

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

我通过添加

@XmlRootElement(name=“abc_xxx”)
到根类。
(其中abc_xxx是XML的根标记)


eclipse生成的JAXB类没有将此注释添加到我的根类中。

使用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();
}
JAXBContext JAXBContext=null;
试一试{
jaxbContext=jaxbContext.newInstance(ObjectFactory.class);
}捕获(JAXBEException e){
e、 printStackTrace();
}
JAXBElement applicationElement=null;
试一试{
applicationElement=(JAXbeElement)
unmarshaller.unmarshal(Thread.currentThread().getClass())
.getResourceAsStream(文件名));
}捕获(JAXBEException e){
e、 printStackTrace();
}

尝试此操作将解决上述问题。我的问题已经解决。

你应该去掉前两行。或者使用另一个对象“DeviceInventory2Result”使其成为根元素,并将Obj123作为子元素添加到其中。不幸的是,我根本无法编辑XML。做另一门课是唯一的方法吗?没有办法忽略它们?我想,你可以忽略子元素,但不能忽略父元素。好吧,酷。我经历了这一切,一切都结束了。似乎就是我要找的。然而,一旦我实现了它,我就得到了以下错误:无法为START\u文档事件调用方法getLocalName()。知道为什么会发生这种情况吗?@CameronJones-在调用getLocalName()之前,您需要先通过START\u文档事件。啊,好的。我错过了你的第一个.nextTag()电话。我修复了这个问题,但是现在我得到了以下错误。错误:javax.xml.bind.UnmarshalException-带有链接异常:[javax.xml.bind.UnmarshalException:意外元素(uri:,本地:“Obj123”)。预期元素为(无)]。我认为这更多的是我的Obj123类的问题,而不是解组过程的问题。@CameronJones-你是如何创建你的
JAXBContext
?@CameronJones-我已经用你需要的信息更新了我的答案,来解决这个问题。哇,这是一个了不起的解决方案。我想知道为什么不自动添加此注释?谢谢,这也是我的解决方案
JAXBContext.newInstance(ObjectFactory.class)
是正确的,但是您仍然需要将结果对象强制转换为
(JAXBElement)
,而不是
(JAXBElement)
。不过这是个好消息
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();
}