Retrieve value from CDATA

2019-07-23 11:39发布

问题:

I am using java(JAXB) and i want to retrieve data from CDATA

<![CDATA[Need Help]]>

Desired output

Need Help

Can any body help me out. I tried several solutions.

Thanks!!

回答1:

try this

@XmlAccessorType(XmlAccessType.FIELD)
public class Test0 {
    String e1;

    public static void main(String[] args) throws Exception {
        String xml = "<root><e1><![CDATA[Need Help]]></e1></root>";
        Test0 t = JAXB.unmarshal(new StringReader(xml), Test0.class);
        System.out.println(t.e1);
    }
}

output

Need Help