Eduardo
Eduardo

Reputation: 1199

Unmarshall "XML element" into string attribute

Using JAXB I would like to unmarshall the next xml:

<con:fault xmlns:con="http://www.bea.com/wli/sb/context">
  <con:errorCode>BEA-382505</con:errorCode>
  <con:reason>OSB Validate action failed validation</con:reason>
  <con:details>
      .... MORE XML tags ...
  <con:details>
</con:fault>

I would like to unmarshall in an object like the next one:

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Fault", propOrder = {
 "errorCode",
 "reason",
 "details"
})
public class Fault {

  @XmlElement(required = true)
  protected String errorCode;
  @XmlElement(required = true)
  protected String reason;
  @XmlElement(required = true)
  protected String details;

  ... getters and setters ...
}

Errorcode and reason attributes are populated properly, but details attribute is not populated with the XML content that is inside "con:details' element.

Thanks

Upvotes: 0

Views: 522

Answers (1)

Dale K James
Dale K James

Reputation: 204

If <con:details> tag has child XML elements, details variable should be an object of Details class, not a String. Details class should contain the child elements of <con:details> tag.

EDIT:

@XmlJavaTypeAdapter can be used to define a XMLAdapter for custom marshaling.

Fault.class

@XmlRootElement
public class Fault {
    @XmlElement(required = true)
    protected String errorCode;

    @XmlElement(required = true)
    protected String reason;

    @XmlElement(name = "details")
    @XmlJavaTypeAdapter(value = DetailsHandler.class)
    protected String details;
}

DetailsHandler.class

public class DetailsHandler extends XmlAdapter<Object, String> {

    /**
     * Factory for building DOM documents.
     */
    private final DocumentBuilderFactory docBuilderFactory;
    /**
     * Factory for building transformers.
     */
    private final TransformerFactory transformerFactory;

    public DetailsHandler() {
        docBuilderFactory = DocumentBuilderFactory.newInstance();
        transformerFactory = TransformerFactory.newInstance();
    }

    @Override
    public String unmarshal(Object o) throws Exception {
        Element titleElement = (Element) o;

        // If there is no child nodes, return empty string
        if (!titleElement.hasChildNodes()) {
            return "";
        }

        // Getting the "details" child elements
        NodeList anchorElements = titleElement.getChildNodes();

        StringWriter stringWriter = new StringWriter();
        StreamResult result = new StreamResult(stringWriter);

        for (int i = 0; i < anchorElements.getLength()-1; i++) {

            // Fetching the child elements one by one
            Element anchor = (Element) anchorElements.item(i);
            // Creating a DOMSource as input for the transformer
            DOMSource source = new DOMSource(anchor);
            // Default transformer: identity tranformer (doesn't alter input)
            Transformer transformer = transformerFactory.newTransformer();
            // This is necessary to avoid the <?xml ...?> prolog
            transformer.setOutputProperty("omit-xml-declaration", "yes");
            // Transform to a StringWriter
            transformer.transform(source, result);
        }
        // Returning result as string
        return stringWriter.toString();
    }

    @Override
    public Object marshal(String s) throws Exception {
        // custom implementation if required
        return null;
    }
}

This link can provide further details.

Upvotes: 1

Related Questions