如何将org.w3c.dom.Element xml节点对象转化成XML格式的string字符串
其中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 resource="OrderUiLabels"/>
</type-validate>
其中class="org.ofbiz.base.util.UtilValidate"为此节点的默认值