org.w3c.dom.Document doc;  
Element root= doc.createElement("root");//注意该createElement与jsp创建的标记方法是一样的.
root.setAttribute("state",state);
root.setAttribute("message",message);
doc.appendChild(root);
org.apache.xml.serialize.OutputFormat format = new OutputFormat(SrcDoc);
format.setEncoding("UTF-8");
java.io.StringWriter stringOut = new StringWriter();
org.apache.xml.serialize.XMLSerializer serial = new XMLSerializer(stringOut, format);
serial.asDOMSerializer();
serial.serialize(SrcDoc.getDocumentElement());
return stringOut.toString();这是将dom转换为String,请高手解答下上面各个语句的意思.可以补加给分
我的qq280840508
上面的大概意思我是明白的,但是具体的用意比如format,serial 这2个对象就不明白什么意思了(有什么用)
还有就是createElement("root") 为什么是root
还有setAttribute() 是怎么工作的,什么意思
既然是doc.createElement 为什么还要用appendChild添加
还有既然doc包含了如上 为什么还要用
serial.asDOMSerializer();
serial.serialize(SrcDoc.getDocumentElement());多谢

解决方案 »

  1.   

     doc转化为string:   
                  DOMSource   domSource   =   new   DOMSource(doc);   
                StringWriter   writer   =   new   StringWriter();   
                StreamResult   result   =   new   StreamResult(writer);   
                TransformerFactory   tf   =   TransformerFactory.newInstance();   
                Transformer   transformer   =   tf.newTransformer();   
                transformer.transform(domSource,   result);   
                System.out.println(   writer.toString());  
    这是我网上搜到的 方法.
    上面的都是jdk自带的么????
      

  2.   

    没有说要加包的都是jdk自带的阿