What is the Jaxb equivalent of a Text node value?

2020-06-08 13:09发布

I am looking to convert a class that looks like this ...

public class Amenity {
   public String id;
   public String value;
}

into the following XML using JaxB annotations:

<amenity id="id-string-here">value-string-here</amenity>

Does anyone know what annotation to use on the value member variable to accomplish this? The closest I've gotten so far is:

@XmlRootElement
public class Amenity {
   @XmlAttribute
   public String id;
   @XmlElement
   public String value;
}

Unfortunately this approach doesn't allow me to specify that the value member variable should not be rendered as its own tag <value></value>.

4条回答
等我变得足够好
2楼-- · 2020-06-08 13:44

I'm not 100% sure about this, but try to use an @XmlValue annotation instead of @XmlElement.

查看更多
混吃等死
3楼-- · 2020-06-08 13:46

It looks like the question was referring to text nodes not CDATA nodes, but here is a link on how EclipseLink JAXB (MOXy) handles CDATA:

查看更多
▲ chillily
4楼-- · 2020-06-08 13:47

This documentation writes:

Q. How can I cause the Marshaller to generate CDATA blocks?

A. This functionality is not available from JAXB directly, but you can configure an Apache Xerces-J XMLSerializer to produce CDATA blocks. Please review the JaxbCDATASample.java sample app for more detail.

(btw, this does not answer your particular question, but since the question title is misleading, and this is the first google result for jaxb CDATA, I'm answering a bit different question)

查看更多
聊天终结者
5楼-- · 2020-06-08 14:00

JAXB does not support marshaling/marshaling to/from CDATA xml types.

查看更多
登录 后发表回答