I am marshalling my Object in 2 separate steps. One adds Header and the other one adds the Body. Now when I use this code
我用两个单独的步骤对对象进行编组。一个添加标题,另一个添加正文。当我使用这个代码时
marshaller.marshal(payload, writer);
//payload is Objects name and writer is StringWriter class object
The XML tag, <?xml version="1.0" encoding="utf-8"?>
is added twice in the final output file.
XML标记,< ?xml version = " 1.0 " encoding = " utf - 8 " ?>在最终输出文件中添加了两次。
How can I not add the [<?xml version="1.0" encoding="utf-8"?>]
XML tag second time when I am marshalling the body part??
如何不添加[
I have used all the properties of Marshaller interface, but that did not help.
我已经使用了Marshaller接口的所有属性,但这并没有帮助。
2 个解决方案
#1
4
You need to do the following:
你需要做以下工作:
- Manually write the root element (not using JAXB)
- 手工编写根元素(不使用JAXB)
- Marshal the Header object. The root element should be the local root element for the header.
- 元帅头对象。根元素应该是头的本地根元素。
- Marshal the Body object. The root element should be the local root element for the body.
- 元帅人体对象。根元素应该是主体的本地根元素。
- Manually close the root element (not using JAXB)
- 手动关闭根元素(不使用JAXB)
If possible use a StAX XMLStreamWriter
to do the manual writing and the marshalling. I have a related example on my blog:
如果可能的话,使用StAX XMLStreamWriter进行手工编写和编组。我在我的博客上有一个相关的例子:
- http://blog.bdoughan.com/2012/08/handle-middle-of-xml-document-with-jaxb.html
- http://blog.bdoughan.com/2012/08/handle-middle-of-xml-document-with-jaxb.html
Note:
注意:
When you marshal into an XML document you must specify the following property on the Marshaller
.
在编组到XML文档时,必须在编组器上指定以下属性。
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
#2
3
The solution to this problem was quite simpler than writing my own code.
解决这个问题的方法比编写我自己的代码要简单得多。
You need to specify JAXB_FRAGMENT property to true on the Marshaller to avoid this problem. This property lets JAXB know it's marshalling into the middle of a document and that it shouldn't write the header.
您需要在编组器上指定JAXB_FRAGMENT属性为true,以避免这个问题。这个属性让JAXB知道它正在编组到文档的中间,并且不应该写入头。
So I kept below code, just before writing the BODY part :
所以在写正文部分之前,我一直在代码下面:
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
And it works like a charm!
它就像一种魅力!
#1
4
You need to do the following:
你需要做以下工作:
- Manually write the root element (not using JAXB)
- 手工编写根元素(不使用JAXB)
- Marshal the Header object. The root element should be the local root element for the header.
- 元帅头对象。根元素应该是头的本地根元素。
- Marshal the Body object. The root element should be the local root element for the body.
- 元帅人体对象。根元素应该是主体的本地根元素。
- Manually close the root element (not using JAXB)
- 手动关闭根元素(不使用JAXB)
If possible use a StAX XMLStreamWriter
to do the manual writing and the marshalling. I have a related example on my blog:
如果可能的话,使用StAX XMLStreamWriter进行手工编写和编组。我在我的博客上有一个相关的例子:
- http://blog.bdoughan.com/2012/08/handle-middle-of-xml-document-with-jaxb.html
- http://blog.bdoughan.com/2012/08/handle-middle-of-xml-document-with-jaxb.html
Note:
注意:
When you marshal into an XML document you must specify the following property on the Marshaller
.
在编组到XML文档时,必须在编组器上指定以下属性。
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
#2
3
The solution to this problem was quite simpler than writing my own code.
解决这个问题的方法比编写我自己的代码要简单得多。
You need to specify JAXB_FRAGMENT property to true on the Marshaller to avoid this problem. This property lets JAXB know it's marshalling into the middle of a document and that it shouldn't write the header.
您需要在编组器上指定JAXB_FRAGMENT属性为true,以避免这个问题。这个属性让JAXB知道它正在编组到文档的中间,并且不应该写入头。
So I kept below code, just before writing the BODY part :
所以在写正文部分之前,我一直在代码下面:
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
And it works like a charm!
它就像一种魅力!