apache的xerces项目中包含一个org.apache.xml.serialize的包
其中有一个类XMLSerializer,可以输出

解决方案 »

  1.   

    //使用 XSLT 来保存 XML 文档
                TransformerFactory tFactory = TransformerFactory.newInstance();
                Transformer transformer = tFactory.newTransformer();
                transformer.setOutputProperty(OutputKeys.INDENT, "yes");
                transformer.setOutputProperty(OutputKeys.ENCODING, "gbk");
                transformer.transform(new DOMSource(doc), new StreamResult(os));
      

  2.   

    除了os和writer,DOM能直接转成String吗?
      

  3.   

    应该有dom的formatter把。。至少jdom是这么输出的
      

  4.   

    以下转成 String//使用 XSLT 来保存 XML 文档
                ByteArrayOutputStream os = new ByteArrayOutputStream();
                TransformerFactory tFactory = TransformerFactory.newInstance();
                Transformer transformer = tFactory.newTransformer();
                transformer.setOutputProperty(OutputKeys.INDENT, "yes");
                transformer.setOutputProperty(OutputKeys.ENCODING, "gbk");
                transformer.transform(new DOMSource(doc), new StreamResult(os));
                String s = new String(os.toByteArray(), encoding);
      

  5.   

    toString();
    可以满足要求吗?