Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop Marshaller class adding XML tag in my output file

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.

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.

like image 668
Dish Avatar asked Aug 21 '14 14:08

Dish


People also ask

What is marshalling in XML?

Object/XML Mapping, or O/X mapping for short, is the act of converting an XML document to and from an object. This conversion process is also known as XML Marshalling, or XML Serialization.

What does Jaxb Marshaller do?

JAXB provides a fast and convenient way to marshal (write) Java objects into XML and unmarshal (read) XML into objects. It supports a binding framework that maps XML elements and attributes to Java fields and properties using Java annotations.

What is Jaxb_fragment?

The -jaxb. fragment command determines whether the marshaller generates document-level events in the XML data. This command is optional. If you omit it, the default is false. Document-level events are not generated in the XML data.

What is the use of Marshaller?

"The Marshaller provides two styles of callback mechanisms that allow application specific processing during key points in the unmarshalling process. In 'class defined' event callbacks, application specific code placed in JAXB mapped classes is triggered during marshalling.


2 Answers

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.

So I kept below code, just before writing the BODY part :

marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);

And it works like a charm!

like image 198
Dish Avatar answered Sep 20 '22 19:09

Dish


You need to do the following:

  1. Manually write the root element (not using JAXB)
  2. Marshal the Header object. The root element should be the local root element for the header.
  3. Marshal the Body object. The root element should be the local root element for the body.
  4. Manually close the root element (not using JAXB)

If possible use a StAX XMLStreamWriter to do the manual writing and the marshalling. I have a related example on my blog:

  • 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.

marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
like image 21
bdoughan Avatar answered Sep 18 '22 19:09

bdoughan