我知道类似的事情已被多次询问,但我无法找到适合我情况的解决方案。
我使用java(StringBuffer)在XML中生成CData部分,并且我正在输出一个简单的HTML代码,如下所示:
public String createXML(OrderDetailBean orderBean) throws ParserConfigurationException {
logger.info("Starting to Create the XML");
getConnectionProperties(); //Load properties file and set the Connection parameters
// Create document
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = dbf.newDocumentBuilder();
Document doc = builder.newDocument();
//Configuring the Factory to get a validating parser (ie one that understands name and spaces)
dbf.setNamespaceAware(true);
dbf.setValidating(true);
//Create doc type
DOMImplementation domImpl = doc.getImplementation();
DocumentType doctype = domImpl.createDocumentType("paymentService", "-//CompanyName//DTD CompanyName PaymentService v1//EN", "http://dtd.CompanyName.com/Service_v1.dtd");
doc.appendChild(doctype);
/******** Add ROOT element: PaymentService ********/
Element rootElement = doc.createElement("paymentService");
//Add Attributes to the Root Element
rootElement.setAttribute("version", "1.4");
rootElement.setAttribute("Code", Code);
/******** Add first element: submit ********/
Element elementSubmit = doc.createElement("submit");
/******** Add second element: order *******/
Element elementOrder = doc.createElement("order");
elementOrder.setAttribute("orderCode", ""+System.currentTimeMillis());
// Add THIRD child element for CData
Element elementOrderContent = doc.createElement("orderContent");
StringBuffer orderContent = new StringBuffer();
orderContent.append("<![CDATA[<center><table> <tr><td class=\"one width190\" align=\"left\" valign=\"top\">");
orderContent.append("<span style=\" font-family: Arial, Helvetica, sans-serif; font-size: 12pt; color: #002469;\">");
orderContent.append("Product:</span> </td><tr><td class=\"one\" align=\"left\" valign=\"top\"><span style=\" font-family: Arial, Helvetica, sans-serif; font-size: 12pt; color: #002469;\">");
orderContent.append("<strong>Product title</strong></span></td></tr> </table></center>]]>");
logger.info("The orderContent Element in XML : "+orderContent.toString());
Text orderContentText = doc.createTextNode(orderContent.toString());
logger.debug("Converted Text for Order Content is: "+orderContentText);
elementOrderContent.appendChild(orderContentText);
elementOrder.appendChild(elementOrderContent); //Add third Order Child: OrderContent
elementSubmit.appendChild(elementOrder); //Add Order Element to Submit
rootElement.appendChild(elementSubmit); //Add First Element (Submit) to Root Element (PaymentService)
doc.appendChild(rootElement); //Add Root Element to XML Doc
String stringXML = convertDocintoString(doc); //print the XML to File
logger.info("The XML Generated is: " + stringXML);
return stringXML;
}
这部分没问题。然后我使用XMLSerializer将XML(XML Document)转换为String,如下所示:
/*
* Convert the XML Document into a String: Serialize DOM Document to generate the xml String
*/
public String convertDocintoString(Document doc) {
logger.info("Converting the XML Document into String XML");
//OutputFormat format = new OutputFormat(doc);
OutputFormat format = new OutputFormat(doc, "UTF-8", true);
//format.setIndenting(true);
XMLSerializer serializer;
String outXML = null;
try {
StringWriter stringOut = new StringWriter ();
serializer = new XMLSerializer(stringOut, format);
serializer.asDOMSerializer();
serializer.serialize(doc);
outXML = stringOut.toString();
logger.debug("The XML String IS: " + outXML);
}
catch (FileNotFoundException e) {
e.printStackTrace();
logger.debug("XML Document Not Found for Serialization!", e);
}
catch (IOException e) {
e.printStackTrace();
logger.debug((new StringBuilder("Issues when converting the XML Document into String XML")).append(e).toString());
}
return outXML;
}
在上面的这一步中,我发现所有的&#39;&lt;&#39;和&#39;&gt;&#39;标记会被<
和>
替换。但我相信这是正常的。
现在,当我试图在HTML页面中显示该CData块时,该CData块将被呈现为实际文本而不是实际HTML,即完全与我上面粘贴的第一个代码块一样。有人请建议什么事发生在这里,我做错了什么? HTML输出是:
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<META http-equiv='Pragma' content='no-cache'>
<META http-equiv='Expires' content='0'>
<title>Select Method</title>
<style type="text/css" media="screen"> @import url(/pictures/dispatcher.css);</style>
<script type="text/javascript" src="/jsp/js/jquery-1.6.2.min.js"></script>
</head>
<body >
<div id="ordercontainer"><font ><b>Your Details</b></font>
<br/><font ><![CDATA[<input type="hidden" name="MC_mycustomvar" value="M_ and MC_ combined"><center><table><tr><td class="one width190" align="left" valign="top"><span style=" font-family: Arial, Helvetica, sans-serif; font-size: 12pt; color: #002469;">Product:</span>&nbsp;&nbsp;</td><tr><td class="one" align="left" valign="top"><span style=" font-family: Arial, Helvetica, sans-serif; font-size: 12pt; color: #002469;"><strong>Product title</strong></span></td></tr></table></center>]]></font><br/>
</body>
</html>
由于
答案 0 :(得分:1)
您需要使用方法org.w3c.dom.Document.createCDATASection(String data)
您在data参数中传递的任何内容都应该包含在结果节点中的CDATA中。
// Add THIRD child element for CData
Element elementOrderContent = doc.createElement("orderContent");
StringBuffer orderContent = new StringBuffer();
// Note: Removed the <![CDATA[ ]]> from this string concat
orderContent.append("<center><table> <tr><td class=\"one width190\" align=\"left\" valign=\"top\">");
orderContent.append("<span style=\" font-family: Arial, Helvetica, sans-serif; font-size: 12pt; color: #002469;\">");
orderContent.append("Product:</span> </td><tr><td class=\"one\" align=\"left\" valign=\"top\"><span style=\" font-family: Arial, Helvetica, sans-serif; font-size: 12pt; color: #002469;\">");
orderContent.append("<strong>Product title</strong></span></td></tr> </table></center>");
logger.info("The orderContent Element in XML : "+orderContent.toString());
// HERE IS THE UPDATED LINE
Text orderContentText = doc.createCDATASection(orderContent.toString());
logger.debug("Converted Text for Order Content is: "+orderContentText);
elementOrderContent.appendChild(orderContentText);
elementOrder.appendChild(elementOrderContent); //Add third Order Child: OrderContent