user2595169
user2595169

Reputation: 145

How to trim String during JAXB MARSHALLING

I am using JAXB to marshal Java Objects to XML.

http://highlevelbits.com/2009/08/jaxb-with-maven_16.html

I have tried setting the collapsedstringadapter Adapter to my JAXB generated classes. But it appears collapsedstringadapter will only Trim the String values during unmarshal and during marshal it returns the String un-trimmed.

Can some one point to me on how to trim String during marshal via JAXB?

Upvotes: 1

Views: 1061

Answers (1)

lexicore
lexicore

Reputation: 43651

Write and configure your own adapter? Just as you'd configure CollapsedStringAdapter?

<?xml version="1.0" encoding="UTF-8"?>
<jaxb:bindings version="1.0" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc">
    <jaxb:bindings schemaLocation="myschema.xsd" node="/xs:schema">
        <jaxb:globalBindings>
            <xjc:javaType name="java.lang.String" xmlType="xs:string"
                adapter="com.acme.foo.bind.annotation.adapters.BiCollapsedStringAdapter"/>
        </jaxb:globalBindings>
    </jaxb:bindings>
</jaxb:bindings>

Upvotes: 1

Related Questions