当前位置: 首页 > 知识库问答 >
问题:

JAXB反编组错误:需要的元素是

庄实
2023-03-14
/*  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"))));
<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中的内容和对象的期望?

共有1个答案

暴阳州
2023-03-14

错误中的“{}”到底指的是什么

{}mynoderespons中,{}部分指的是没有设置命名空间URI部分的限定名。

您需要使用包级别@xmlschema注释映射命名空间限定:

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

import javax.xml.bind.annotation.XmlNsForm;
import javax.xml.bind.annotation.XmlSchema;
    null
 类似资料:
  • 我正在编写XSD来验证XML,但在验证时出现了以下错误: 输出错误 使用XML架构验证当前文件: 错误:元素'{http://www.w3.org/2001/XMLSchema-instance}Gasto':不应使用此元素。预计为(加斯托) ...我不明白这个错误 以下是我的XML示例: 下面是我的XSD示例:

  • Borrower类使用基于XSD的JaxB解析,使用JDK1.7和Eclipse IDE。 : } XSD: XML:

  • 我有几个关于JAXB编组的简单问题。我正在尝试封送包含以下字段的类: 只需使用以下序列化代码: 我得到的输出是: 现在,我面临的问题如下: > 我想要名称空间xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance“出现在根元素中,即TokenRestrictionTemplate中,而不是单个子元素中。如何实现这一点? 我有一些元素,例如带有@Xm

  • jaxb 将所有标签分组为一个,我需要每个标签都有自己的命名空间。 具有: 需要: 将命名空间添加到@XmlElement没有帮助 包装信息:

  • 背景: 我使用JAXB将XML解组为Java对象。最初,我只是使用JAXB来执行解组。然后对代码进行静态分析,并提出了XML外部实体注入的高关键性问题。经过一点研究,我发现了一个建议(https://www.owasp.org/index.php/XML_External_Entity_(XXE)_Prevention_Cheat_Sheet#JAXB_Unmarshaller)使用配置为防止解析