JAXB unmarshal元素子为纯文本

时间:2014-12-02 16:45:08

标签: java xml jaxb unmarshalling xjc

我想知道是否/如何解组xml文档,以便某些已知元素的内容不会被视为xml而是纯文本。

运行以下示例时,输出为

MessageData [text= and this is just plain]

但我希望它包含

MessageData [text= This is <b>bold</b>, this is <i>italic</i> and this is just plain]

import java.io.StringReader;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlRootElement;

public class JAXBTest {

    public static String xml = "<messageData><text>This is <b>bold</b>, this is <i>italic</i> and this is just plain</text></messageData>";

    public static void main(String[] args) throws Exception {
        JAXBContext context = JAXBContext.newInstance(MessageData.class);

        Unmarshaller unmarshaller = context.createUnmarshaller();

        System.out.println(unmarshaller.unmarshal(new StringReader(xml)));
    }

    @XmlRootElement
    static class MessageData {
        public String text;

        @Override
        public String toString() {
            StringBuilder builder = new StringBuilder();
            builder.append("MessageData [text=");
            builder.append(text);
            builder.append("]");
            return builder.toString();
        }
    }
}

提前致谢

0 个答案:

没有答案