Note that there are some explanatory texts on larger screens.

plurals
  1. POJAXB marshals XML differently to OutputStream vs. StringWriter
    primarykey
    data
    text
    <p>I apologize if this has been answered, but the search terms I have been using (i.e. <strong><em>JAXB @XmlAttribute condensed</em></strong> or <strong><em>JAXB XML marshal to String different results</em></strong>) aren't coming up with anything.</p> <p>I am using JAXB to un/marshal objects annotated with <code>@XmlElement</code> and <code>@XmlAttribute</code> annotations. I have a formatter class which provides two methods -- one wraps the marshal method and accepts the object to marshal and an <code>OutputStream</code>, the other just accepts the object and returns the XML output as a String. Unfortunately, these methods do not provide the same output for the same objects. When marshaling to a file, simple object fields internally marked with <code>@XmlAttribute</code> are printed as: </p> <pre><code>&lt;element value="VALUE"&gt;&lt;/element&gt; </code></pre> <p>while when marshaling to a String, they are: </p> <pre><code>&lt;element value="VALUE"/&gt; </code></pre> <p>I would prefer the second format for both cases, but I am curious as to how to control the difference, and would settle for them being the same regardless. I even created one static marshaller that both methods use to eliminate different instance values. The formatting code follows:</p> <pre><code>/** Marker interface for classes which are listed in jaxb.index */ public interface Marshalable {} </code></pre> <hr> <pre><code>/** Local exception class */ public class XMLMarshalException extends BaseException {} </code></pre> <hr> <pre><code>/** Class which un/marshals objects to XML */ public class XmlFormatter { private static Marshaller marshaller = null; private static Unmarshaller unmarshaller = null; static { try { JAXBContext context = JAXBContext.newInstance("path.to.package"); marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); unmarshaller = context.createUnmarshaller(); } catch (JAXBException e) { throw new RuntimeException("There was a problem creating a JAXBContext object for formatting the object to XML."); } } public void marshal(Marshalable obj, OutputStream os) throws XMLMarshalException { try { marshaller.marshal(obj, os); } catch (JAXBException jaxbe) { throw new XMLMarshalException(jaxbe); } } public String marshalToString(Marshalable obj) throws XMLMarshalException { try { StringWriter sw = new StringWriter(); return marshaller.marshal(obj, sw); } catch (JAXBException jaxbe) { throw new XMLMarshalException(jaxbe); } } } </code></pre> <hr> <pre><code>/** Example data */ @XmlType @XmlAccessorType(XmlAccessType.FIELD) public class Data { @XmlAttribute(name = value) private String internalString; } </code></pre> <hr> <pre><code>/** Example POJO */ @XmlType @XmlRootElement(namespace = "project/schema") @XmlAccessorType(XmlAccessType.FIELD) public class Container implements Marshalable { @XmlElement(required = false, nillable = true) private int number; @XmlElement(required = false, nillable = true) private String word; @XmlElement(required = false, nillable = true) private Data data; } </code></pre> <p>The result of calling <code>marshal(container, new FileOutputStream("output.xml"))</code> and <code>marshalToString(container)</code> are as follows:</p> <p><strong>Output to file</strong> </p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt; &lt;ns2:container xmlns:ns2="project/schema"&gt; &lt;number&gt;1&lt;/number&gt; &lt;word&gt;stackoverflow&lt;/word&gt; &lt;data value="This is internal"&gt;&lt;/data&gt; &lt;/ns2:container&gt; </code></pre> <p>and</p> <p><strong>Output to String</strong> </p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt; &lt;ns2:container xmlns:ns2="project/schema"&gt; &lt;number&gt;1&lt;/number&gt; &lt;word&gt;stackoverflow&lt;/word&gt; &lt;data value="This is internal"/&gt; &lt;/ns2:container&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload