首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > XML SOAP >

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

2012-10-31 
如何将org.w3c.dom.Element xml节点对象转化成XML格式的string字符串其中org.w3c.dom.Element xml节点的内

如何将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"为此节点的默认值

热点排行