Hi,
if I parse and load an CDATA like <!CDATA[]> into a DOM the result (as a String) is empty. Is this normal?
If yes, is there a workround for this problem?
Thanks.
this is my code:
xmlDoc = new XMLDocument();
xmlDoc.fromString("<test><p0><![CDATA[xxx]]></p0><p1><![CDATA[]]></p1></test>");
System.out.println(xmlDoc.toString());
and this is the result: <test><p0><![CDATA[xxx]]></p0><p1/></test>
the methods of my class XMLDocument are:
public void fromString(String text) throws P2XMLException
{
DocumentBuilderFactory dbf;
DocumentBuilder db;
Transformer transformer;
StreamResult result;
try {
dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
dbf.setValidating(true);
dbf.setAttribute("http://apache.org/xml/features/validation/dynamic",
new Boolean(true));
dbf.setAttribute("http://apache.org/xml/features/validation/schema",
new Boolean(true));
db = dbf.newDocumentBuilder();
db.setErrorHandler(new XMLDocument.XmlErrorHandler());
doc = db.parse(new InputSource(new StringReader(text)));
node = doc;
} catch (ParserConfigurationException pce) {
throw new P2XMLException(pce.getMessage());
} catch(IOException ioe) {
throw new P2XMLException(ioe.getMessage());
} catch(org.xml.sax.SAXException se) {
throw new P2XMLException(se.getMessage());
}
} // fromString
public String toString()
{
StreamResult streamResult;
Transformer transformer;
String result = null;
try {
streamResult = new StreamResult(new StringWriter());
transformer = TransformerFactory.newInstance().newTransformer();
transformer.transform(new DOMSource(node), streamResult);
result = streamResult.getWriter().toString();
result = com.apag.p2plus.p2core.Text.replace(result, "encoding=\"UTF-8\"", "", false);
result = com.apag.p2plus.p2core.Text.replace(result, "<?xml version=\"1.0\" ?>", "", false);
return(result);
} catch (TransformerException tce) {
return(tce.getMessage());
}
} // toString
