JAXB解组错误:预期的元素是< {} Root> [英] JAXB unmarshalling error: Expected elements are <{ } Root>

查看:228
本文介绍了JAXB解组错误:预期的元素是< {} Root>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在重复使用其他地方生成的现有对象来解组以String类型出现的XML数据。

I'm reusing existing objects generated elsewhere to unmarshall XML data coming in as a String type.

对象:

/*  3:   */ import java.util.ArrayList;
/*  4:   */ import java.util.List;
/*  5:   */ import javax.xml.bind.annotation.XmlAccessType;
/*  6:   */ import javax.xml.bind.annotation.XmlAccessorType;
/*  7:   */ import javax.xml.bind.annotation.XmlElement;
/*  8:   */ import javax.xml.bind.annotation.XmlRootElement;
/*  9:   */ import javax.xml.bind.annotation.XmlType;
/* 10:   */ 
/* 11:   */ @XmlAccessorType(XmlAccessType.FIELD)
/* 12:   */ @XmlType(name="", propOrder={"policy"})
/* 13:   */ @XmlRootElement(name="MyNodeResponse")
/* 14:   */ public class MyNodeResponse
/* 15:   */ {
/* 16:   */   @XmlElement(name="Policy")
/* 17:   */   protected List<Policy> policy;
/* 18:   */   
/* 19:   */   public List<Policy> getPolicy()
/* 20:   */   {
/* 21:65 */     if (this.policy == null) {
/* 22:66 */       this.policy = new ArrayList();
/* 23:   */     }
/* 24:68 */     return this.policy;
/* 25:   */   }
/* 26:   */ }

我的解组代码:

JAXBContext jc = JAXBContext.newInstance(MyNodeResponse.class); 
Unmarshaller unmarshaller = jc.createUnmarshaller();
MyNodeResponse myNodeResponse = (MyNodeResponse)unmarshaller.unmarshal(new InputSource(new ByteArrayInputStream(xmlStringInput.getBytes("utf-8"))));

我的输入XML:

<ns2:MyNodeResponse 
         xmlns:ns2="mynamespace/2010/10">   
   <ns2:Policy>
   ....more data....
   <ns2:Policy/>
<ns2:MyNodeResponse />

解组时出现以下错误:

unexpected element (uri:"mynamespace/2010/10", local:"MyNodeResponse"). Expected elements are <{}MyNodeResponse>

{}在错误中引用了什么,以及如何解组?匹配输入XML中存在的内容以及对象期望的方式?

What exactly does the "{ }" refer to in the error, and how do I unmarshall in a way to match what is present in the input XML and how the object is expecting?

推荐答案

错误消息说的是什么



What the Error Message is Saying


{}在错误中引用了什么?

What exactly does the "{ }" refer to in the error

{} MyNodeRespons {} 部分是指没有名称空间URI的限定名称部分集。

In {}MyNodeRespons the {} portion refers to that qualified name not having the namespace URI portion set.

您需要使用包级别<$映射命名空间限定c $ c> @XmlSchema 注释:

You need to map the namespace qualification using the package level @XmlSchema annotation:

package-info.java

@XmlSchema(
    namespace = "mynamespace/2010/10",
    elementFormDefault = XmlNsForm.QUALIFIED)
package example;

import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;

更多信息

  • http://blog.bdoughan.com/2010/08/jaxb-namespaces.html

这篇关于JAXB解组错误:预期的元素是&lt; {} Root&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆