Viswanath T
Viswanath T

Reputation: 69

Issue with marshalling in Java

I want to Marshall a Java object where one of its field have small xml content. Like,

feature="<feat>T12</feat>"; 

When I tried to Marshall this, the xml output is showing up as

&lt; fear &gt; T12 &lt; feat &gt; 

I need the exact value I have above. Can some one help?

Thanks, Viswanath

Upvotes: 0

Views: 453

Answers (2)

Mirko Klemm
Mirko Klemm

Reputation: 2068

If your Java object has a property that contains a piece of XML, consider declaring it as either a defined XML type (if you know the intended schema of that XML snippet), or declare it as an "any" Element or "any" Type, so it will be mapped to a DOM structure in Java, which can be treated structurally correct in your Java code.

If you still and really wish to simply convey XML data in a string, which I do NOT recommend, the representation generated by the marshaller is probably the only one that is acceptable and correct. After unmarshalling, you will get back your XML snippet in exactly the same way as you entered it above in your example, and you're free to deal with it as a string in the same way as it was before marshalling.

I'd recommend you define the schema of the XML snippet in an XSD, then use the JAXB "XML-to-Java compiler" (XJC) to generate Java code for it, which can be easily done automatically if you use maven or a similar build system. You then have to add the appropriate annotations to your Java class that contains this property, and reference the generated class as the type of your property, which will then give you a clean way to marshal and unmarshal everything as a true XML structure (see example below).

Even better, define as many of your data classes as possible in XSD, and use code generation throughout. This will make many things much easier.

Example: XSD definition of value for "feature" property

<xs:element name="feature">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="feat" type="string"/>
      <!-- additional possibilities/elements/whatever... -->
    </xs:sequence>
  </xs:complexType>
</xs:element>

Then, in your Java class, do:

@XmlRootElement
public class ClassWithFeature {
  private Feat feature;

  public Feat getFeature() { return feature; }
  public void setFeature(final Feat feature) { this.feature = feature; }
}

Then initialize it like so:

Feature feat = new Feature();
feat.setFeat("T12");
myOb.setFeature(feat);

There are XJC plugins to make this initialization more convenient, like Fluent-Api, or a builder pattern: jaxb-rich-contract-plugin

After marshalling, the XML will look something like:

<ClassWithFeature>
    <feature>
        <feat>T12</feat>
    </feature>
</ClassWithFeature>

Which is probably what you want...

Upvotes: 0

J&#233;r&#233;mie B
J&#233;r&#233;mie B

Reputation: 11022

characters like < or > are not valid in an attribute in xml. The marshalling operation quote this characters in order to have a valid xml file.

If you unmarshall your document with an xml parser, you'll get the real value. Don't try to read xml without a real xml parser.

Upvotes: 1

Related Questions