简体   繁体   English

有没有比这个代码更优雅的方法将XML文档转换为Java中的String?

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

Here is the code currently used. 这是当前使用的代码。

public String getStringFromDoc(org.w3c.dom.Document doc)    {
        try
        {
           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);
           writer.flush();
           return writer.toString();
        }
        catch(TransformerException ex)
        {
           ex.printStackTrace();
           return null;
        }
    }

Relies on DOM Level3 Load/Save : 依赖DOM Level3加载/保存

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

This is a little more concise: 这更简洁一点:

try {
    Transformer transformer = TransformerFactory.newInstance().newTransformer();
    StreamResult result = new StreamResult(new StringWriter());
    DOMSource source = new DOMSource(doc);
    transformer.transform(source, result);
    return result.getWriter().toString();
} catch(TransformerException ex) {
    ex.printStackTrace();
    return null;
}

Otherwise you could use a library like XMLSerializer from Apache: 否则你可以使用像Apache的XMLSerializer这样的库:

//Serialize DOM
OutputFormat format    = new OutputFormat (doc); 
// as a String
StringWriter stringOut = new StringWriter ();    
XMLSerializer serial   = new XMLSerializer (stringOut,format);
serial.serialize(doc);
// Display the XML
System.out.println(stringOut.toString());

The transformer API is the only XML-standard way to transform from a DOM object to a serialized form (String in this case). 变换器API是从DOM对象转换为序列化形式(在本例中为String)的唯一XML标准方法。 As standard I mean SUN Java XML API for XML Processing . 作为标准,我指的是用于XML处理的 SUN Java XML API

Other alternatives such as Xerces XMLSerializer or JDOM XMLOutputter are more direct methods (less code) but they are framework-specific. 其他替代方法(如Xerces XMLSerializer或JDOM XMLOutputter)是更直接的方法(代码更少),但它们是特定于框架的。

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). 通过使用标准XML Java API,您可以在不更改代码的情况下插入您选择的XML-Parser或XML-Transformer(与JDBC驱动程序相同)。 Is there anything more elegant than that? 还有比这更优雅的东西吗?

You could use XOM to perhaps do this: 您可以使用XOM来执行此操作:

org.w3c.dom.Document domDocument = ...;
nu.xom.Document xomDocument = 
    nu.xom.converters.DOMConverter.convert(domDocument);
String xml = xomDocument.toXML();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM