Java Jaxb:意外元素(uri:“”,本地:“创建”)。预期元素为 <{}Create>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23205243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 21:19:48  来源:igfitidea点击:

Java Jaxb : unexpected element (uri:"", local:"Create"). Expected elements are <{}Create>

javaxmljaxbunmarshallingblind

提问by Hann

I have a prob with my JAXB

我的 JAXB 有问题

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

My xml :

我的 xml :

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

My code java :

我的代码 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");
}

And I have this :

我有这个:

unexpected element (uri:"", local:"Create"). Expected elements are <{http://www.example.org/XSD_Maths}create>

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

And my Code stoped by this :

我的代码由此停止:

 msgObject = decodeur.unmarshal(sr);  

My xml is good ? , Can you help me , because I don't know what's the problem

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

采纳答案by bdoughan

Your XML Schema probably has a schematag like the following.

您的 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">

Since it specifies a targetNamespaceof http://www.example.org/XSD_Maths. Your XML will need to look like the following:

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

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

Note About Unmarshalling from a DOM

关于从 DOM 解组的注意事项

If you unmarshalling from a DOM Documentor Elementmake sure that the DOM parser you used was namespace aware. This is done by setting the following flag on the DocumentBuilderFactory.

如果您从 DOM 解组DocumentElement确保您使用的 DOM 解析器是命名空间感知的。这是通过在DocumentBuilderFactory.

documentBuilderFactory.setNamespaceAware(true);

For More Information

想要查询更多的信息

Below is a link to an article on my blog where I go into more depth about JAXB and namespaces.

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