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

后端 未结 4 1697
轮回少年
轮回少年 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:23

    The transformer API is the only XML-standard way to transform from a DOM object to a serialized form (String in this case). As standard I mean SUN Java XML API for XML Processing.

    Other alternatives such as Xerces XMLSerializer or JDOM XMLOutputter are more direct methods (less code) but they are framework-specific.

    In my opinion the way you have used is the most elegant and most portable of all. By using a standard XML Java API you can plug the XML-Parser or XML-Transformer of your choice without changing the code(the same as JDBC drivers). Is there anything more elegant than that?

提交回复
热议问题