【问题标题】:How to resolve the error 'nexpected element (uri:"", local:"Create"). Expected elements are <{}Create>'? [closed]Java Jaxb:意外元素(uri:“”,本地:“创建”)。预期元素是 <{}Create>
【发布时间】:2014-06-05 23:32:27
【问题描述】:

我的 JAXB 有问题

<element name="create">
    <complexType>
        <sequence>
            <element name="name" type="string"></element>
        </sequence>
    </complexType>
</element>

我的 xml:

<Create>
<name> coco </name>
</Create>

我的代码java:

JAXBContext context = JAXBContext.newInstance("MyPackage");
 Unmarshaller decodeur =    context.createUnmarshaller();
System.out.println("text : " + message);
msgObject = decodeur.unmarshal(sr);  
     if (msgObject instanceof Create)

{
      System.out.println(" action");
}

我有这个:

意外元素(uri:“”,本地:“创建”)。预期的元素是 http://www.example.org/XSD_Maths}create>

我的代码就这样停止了:

 msgObject = decodeur.unmarshal(sr);  

我的 xml 好吗? , 你能帮帮我吗,因为我不知道是什么问题

【问题讨论】:

  • 您的 XML 具有带有大写“C”的“Create”,而您的架构具有带有小写“c”的“create”。问题中的错字,或代码中的错字?

标签: java xml jaxb unmarshalling blind


【解决方案1】:

您的 XML 架构可能有一个 schema 标记,如下所示。

<?xml version="1.0" encoding="UTF-8"?>
<schema 
    xmlns="http://www.w3.org/2001/XMLSchema" 
    targetNamespace="http://www.example.org/XSD_Maths" 
    xmlns:tns="http://www.example.org/XSD_Maths" 
    elementFormDefault="qualified">

因为它指定了http://www.example.org/XSD_MathstargetNamespace。您的 XML 需要如下所示:

<create xmlns="http://www.example.org/XSD_Maths">
    <name> coco </name>
</create>

关于从 DOM 解组的注意事项

如果您从 DOM DocumentElement 解组,请确保您使用的 DOM 解析器是命名空间感知的。这是通过在DocumentBuilderFactory 上设置以下标志来完成的。

documentBuilderFactory.setNamespaceAware(true);

更多信息

下面是我博客上一篇文章的链接,我在该文章中更深入地了解了 JAXB 和命名空间。

【讨论】:

  • Tahnks Blaise,我有相同的架构,但你的 xml 不起作用
  • @user3557873 - 你有不同的例外吗?
  • @user3557873 - 你的sr 变量持有什么类型的对象。
  • 非常感谢 muuccccch Blaise,我重新启动了我的 Eclipse,它正在使用您的 xml。布莱斯你很棒 :) 谢谢谢谢
  • @bdoughan 我也有类似的问题。可以看看吗stackoverflow.com/questions/67975111/…
猜你喜欢
  • 2017-08-14
  • 2018-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-01
  • 1970-01-01
  • 2016-07-12
  • 1970-01-01
相关资源
最近更新 更多