Pergunta

Using MOXy 2.3.1, but could upgrade if it would help.

I have the following XML:

<myelement>
text content <b>mixed</b> with tags
</myelement>

Which I would like to be stored in a String field containing:

text content <b>mixed</b> with tags

I've been going on the idea I'd need to use the XML Transformation feature within MOXy, and my code looks like this:

// Not a root element, it's own mapping annotation
// is defined in another class.
@XmlAccessorType(XmlAccessType.NONE)
class MyElement {

    @XmlTransformation
    @XmlReadTransformer(transformerClass=TempTrans.class)
    @XmlValue
    String markup
}

public class TempTrans implements AttributeTransformer {

    private AbstractTransformationMapping mapping;

    public void initialize(AbstractTransformationMapping mapping) {
        this.mapping = mapping;
    }

    public Object buildAttributeValue(Record record, Object instance, Session session) {
        return null;
    }
}

I've been debugging on the return null; line to see what's available to me in the mapping object. I haven't found it very useful, am I on the right track?

I'm hoping for a mechanism similar to XStream's HierarchicalStreamReader, something to give me DOM-like access to the source XML. Any workaround would be greatly appreciated.

I wouldn't mind ending up with:

text content &lt;b&gt;mixed&lt;/b&gt; with tags

But the source XML would need to have unescaped markup in it.

Thanks, Mike

Foi útil?

Solução

You can use the @XmlAnyElement annotation and specify a DOMHandler to convert the DOM fragment to/from a String value.

For a Complete Example

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top