Java JaxB - 对象到 XML 字符串到对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13289404/
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-09-06 13:45:46  来源:igfitidea点击:

Java JaxB - Object to XML String to Object

xmljaxb

提问by Andez

I have an issue using JaxB in Netbeans 7.1.2.

我在 Netbeans 7.1.2 中使用 JaxB 时遇到问题。

I have auto-generated my classes from a schema using JaxB (New JaxB Binding). I am creating the object that will be serialized to an XML string using the Marshaller and then back to a new object instance from the XML String. However, I get the following exception:

我已经使用 JaxB(新 JaxB 绑定)从模式自动生成了我的类。我正在创建将使用 Marshaller 序列化为 XML 字符串的对象,然后从 XML 字符串返回到新的对象实例。但是,我收到以下异常:

javax.xml.bind.UnmarshalException: unexpected element (uri:"http://www.somewhere.com/some/path", local:"MyQueryComplexTypeQuery"). Expected elements are (none)

The marshalling/serializing to XML string works fine. It's when it is unmarshalled/deserialized that is causing the issue.

编组/序列化到 XML 字符串工作正常。当它被解组/反序列化时,就会导致问题。

I am using the following code to build the object and marshal it to an XML string:

我正在使用以下代码构建对象并将其编组为 XML 字符串:

// build the object
ObjectFactory of = new ObjectFactory();

MyQueryComplexType oaaqc = of.createMyQueryComplexType();
oaaqc.setField1("edit");
oaaqc.setField2("net");
oaaqc.setField3("24");

JAXBElement<MyQueryComplexType> createMyQueryComplexType = of.createMyQueryComplexTypeQuery(oaaqc);

// serialise to xml
StringWriter writer = new StringWriter();
JAXBContext context = JAXBContext.newInstance(MyQueryComplexType.class);            
Marshaller m = context.createMarshaller();
m.marshal(createMyQueryComplexType, writer);

// output string to console
String theXML = writer.toString();
System.out.println(theXML);

This produces the following XML (formatted) in the console:

这会在控制台中生成以下 XML(格式化):

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<MyQueryComplexTypeQuery xmlns="http://www.somewhere.com/some/path">
    <Field1>edit</Field1>
    <Field2>net</Field2>
    <Field3>24</Field3>
</MyQueryComplexTypeQuery>

Now I come to deserialize/unmarshal the string to a new instance MyQueryComplexType with the following code:

现在,我使用以下代码将字符串反序列化/解组为新实例 MyQueryComplexType:

Unmarshaller u = context.createUnmarshaller();
MyQueryComplexTypeQuery o = (MyQueryComplexType) u.unmarshal(new StringReader(theXML));

In the auto generated package-info.java it has the following contents:

在自动生成的 package-info.java 中有以下内容:

@javax.xml.bind.annotation.XmlSchema(namespace = "http://www.somewhere.com/some/path", elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED)
package MyProject.SomeNamespace.MyQuery;

I thought the simple process of object -> string -> object would work. This is the first time I've used JaxB (so be gentle). I have seen other posts mentioning the namespaces, and everything looks ok to me. I've auto generated the classes from the schema, constructed the object, marshalled to xml string. I thought simply reversing the process for unmarshal would be similar.

我认为 object -> string -> object 的简单过程会起作用。这是我第一次使用 JaxB(所以要温柔)。我看过其他帖子提到了命名空间,对我来说一切都很好。我已经从模式中自动生成了类,构造了对象,编组为 xml 字符串。我认为简单地反转解组过程是相似的。

The actual exception is thrown on line:

实际的异常是在线抛出的:

MyQueryComplexTypeQuery o = (MyQueryComplexType) u.unmarshal(new StringReader(theXML));

I thought I was doing the unmarshalling from string to object would be simple. I don't know if I am doing something wrong or missing something. I hope you guys can shed some light or open my eyes.

我以为我正在做从字符串到对象的解组会很简单。我不知道我是做错了什么还是遗漏了什么。我希望你们能给我一些启发或睁开眼睛。

I have cut down the code to simplify what is in my app that causes the error. Namespaces have been changed to protect the identity of things on the web.

我已经减少了代码以简化我的应用程序中导致错误的内容。命名空间已更改以保护网络上事物的身份。

Any thoughts?

有什么想法吗?

Thanks

谢谢

Andez

安德斯

回答by bdoughan

Creating the JAXBContext

创建 JAXBContext

For JAXB models that are generated from an XML schema you could create the JAXBContexton the package name rather than on a particular class:

对于从 XML 模式生成的 JAXB 模型,您可以JAXBContext在包名称而不是特定类上创建:

JAXBContext context = JAXBContext.newInstance("your.package.name");

Not:

不是:

JAXBContext context = JAXBContext.newInstance(MyQueryComplexType.class);  

This will ensure that the ObjectFactoryclass that contains the @XmlElementDeclannotations is processed correctly which is what is missing from your use case.

这将确保正确处理ObjectFactory包含@XmlElementDecl注释的类,这是您的用例中缺少的。

Unmarshal Operation

解组操作

Note that during your marshal operation you needed to wrap your instance of MyQueryComplexTypein a JAXBElement? This is because MyQueryComplexTypewas not annotated with @XmlRootElementand the root element information needed to come from the JAXBElement. This means that when you do the unmarshal operation you will get a JAXBElementback as well.

请注意,在您的 marshal 操作期间,您需要将您的实例包装MyQueryComplexTypeJAXBElement? 这是因为MyQueryComplexType没有注释,@XmlRootElement根元素信息需要来自JAXBElement. 这意味着当您执行解组操作时,您也将获得JAXBElement回报。

JAXBElement<MyQueryComplexTypeQuery> o = (JAXBElement<MyQueryComplexType>) u.unmarshal(new StringReader(theXML));

Not:

不是:

MyQueryComplexTypeQuery o = (MyQueryComplexType) u.unmarshal(new StringReader(theXML));

For More Information

想要查询更多的信息