Is there a more elegant way to convert an XML Document to a String in Java than this code?

后端 未结 4 1704
轮回少年
轮回少年 2020-11-29 18:42

Here is the code currently used.

public String getStringFromDoc(org.w3c.dom.Document doc)    {
        try
        {
           DOMSource domSource = new DOM         


        
4条回答
  •  生来不讨喜
    2020-11-29 19:43

    Relies on DOM Level3 Load/Save:

    public String getStringFromDoc(org.w3c.dom.Document doc)    {
        DOMImplementationLS domImplementation = (DOMImplementationLS) doc.getImplementation();
        LSSerializer lsSerializer = domImplementation.createLSSerializer();
        return lsSerializer.writeToString(doc);   
    }
    

提交回复
热议问题