您的位置:首页 > 其它

如何将org.w3c.dom.Element xml节点对象转化成XML格式的string字符串

2011-08-02 11:27 513 查看
其中org.w3c.dom.Element xml节点的内容为

<type-validate >
<fail-property resource="OrderUiLabels"  property="checkhelper.select_shipping_method"/>
</type-validate>


方法一:

Document document = validate.getOwnerDocument();
DOMImplementationLS domImplLS = (DOMImplementationLS) document.getImplementation();
LSSerializer serializer = domImplLS.createLSSerializer();
String str = serializer.writeToString(validate);
str的内容为:

<?xml version="1.0" encoding="UTF-16"?>
<type-validate>
<fail-property property="checkhelper.select_shipping_destination" resource="OrderUiLabels"/>
</type-validate>

方法二:

TransformerFactory transFactory = TransformerFactory.newInstance();
Transformer transformer = null;
try {
transformer = transFactory.newTransformer();
} catch (TransformerConfigurationException e)
{
e.printStackTrace();
}
StringWriter buffer = new StringWriter();
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
try {
transformer.transform(new DOMSource(validate),new StreamResult(buffer));
} catch (TransformerException e)
{
e.printStackTrace();
}
String s = buffer.toString();

s的内容为



<type-validate class="org.ofbiz.base.util.UtilValidate">
<fail-property property="checkhelper.select_shipping_destination" resource="OrderUiLabels"/>
</type-validate>


其中class="org.ofbiz.base.util.UtilValidate"为此节点的默认值

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: