java adding cdata to xml string

牧云@^-^@ 提交于 2019-12-05 20:15:01

It sounds like you just want:

Node cdata = doc.createCDATASection(text);
parentElement.appendChild(cdata);

This post may be hold but i feel i should respond, this may help someone else.

        JAXBContext context = JAXBContext.newInstance(SignedContent.class);
        Marshaller marshallerObj = context.createMarshaller();
        marshallerObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        StringWriter sw = new StringWriter();
        marshallerObj.marshal(signedContentObj, sw);
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setValidating(true);
        factory.setExpandEntityReferences(false);
        Document doc = factory.newDocumentBuilder().newDocument();
        doc.createCDATASection(sw.toString()).getData();

You can play around from here...

Use Javas + operator:

"<![CDATA[" + "<SignedContent>someparametres</SignedContent>" + "]]>"
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!