1. Introduction
1.导言
XML (eXtensible Markup Language) is one of the most popular schemas for structuring information. Moreover, the parsing and manipulation of XML Documents in Java are commonly accomplished using technologies like DOM (Document Object Model) and SAX (Simple API for XML).
XML(可扩展标记语言) 是最常用的信息结构模式之一。此外,在 Java 中解析和操作 XML Documents 通常使用 DOM (文档对象模型) 和 SAX (Simple API for XML) 等技术来完成。
In some cases, it might be necessary to transform an XML Document object into its string form, which could be used to store the XML information inside a database or to pass through over a network.
在某些情况下,可能需要将 XML Document 对象转换为字符串形式,这种形式可用于将 XML 信息存储在数据库中或通过网络传递。
In this tutorial, we’ll discuss several ways of transforming an XML Document object into a string in Java.
在本教程中,我们将讨论在 Java 中将 XML Document 对象转换为字符串的几种方法。
2. Example
2.示例
Suppose we have the following Document object:
假设我们有以下 Document 对象:
Document document = // ...
Document document = // ...</代码
This Document object represents XML content in memory:
此 Document 对象表示内存中的 XML 内容:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root>
<child1>This is child element 1</child1>
<child2>This is child element 2</child2>
</root>
Now, we need to convert this XML Document object into a Java string.
现在,我们需要将 XML Document 对象转换为 Java 字符串。
3. Using XML Transformation APIs
3.使用 XML 转换 API
The javax.xml.transform package in Java includes classes and interfaces for performing XML transformations. One of its capabilities is the conversion of an XML Document object into a string representation. The following code demonstrates how to use the javax.xml.transform package to parse this XML Document object to a Java string:
Java 中的 javax.xml.transform 包包含用于执行 XML 转换的类和接口。其功能之一是将 XML 文档对象转换为字符串表示。以下代码演示了如何使用 javax.xml.transform 包将 XML Document 对象解析为 Java 字符串:
@Test
public void givenXMLDocument_whenUsingTransformer_thenConvertXMLToString()
throws TransformerException {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
StringWriter stringWriter = new StringWriter();
transformer.transform(new DOMSource(document), new StreamResult(stringWriter));
String result = stringWriter.toString();
assertTrue(result.contains("<root>"));
assertTrue(result.contains("This is child element 1"));
assertTrue(result.contains("This is child element 2"));
}
We first instantiate a TransformerFactory and a Transformer, used for XML transformation. Then, we construct a StringWriter to store the transformed XML in text form. Then, the transform() method changes the XML Document, and we can save it into the result string using the stringWrite.toString() method.
我们首先实例化用于 XML 转换的 TransformerFactory 和 Transformer 。然后,我们构建一个 StringWriter 以文本形式存储转换后的 XML。然后,transform() 方法会改变 XML Document ,我们可以使用 stringWrite.toString() 方法将其保存到 result 字符串中。
4. Using Java XMLBeans
4.使用 Java XMLBeans
Converting between XML Document and string is easy and flexible using the XmlBeans approach in the Java XML manipulation world. We use XmlObject.Factory.parse(document), which parses the XML Document into an XmlObject for subsequent operative activities:
使用 Java XML 操作世界中的 XmlBeans 方法,XML Document 和字符串之间的转换既简单又灵活。我们使用 XmlObject.Factory.parse(document),它将 XML Document 解析为一个 XmlObject 用于后续操作活动:
@Test
public void givenXMLDocument_whenUsingXmlBeans_thenConvertXMLToString() {
try {
XmlObject xmlObject = XmlObject.Factory.parse(document);
XmlOptions options = new XmlOptions();
options.setSavePrettyPrint();
options.setUseDefaultNamespace();
options.setSaveAggressiveNamespaces();
String xmlString = xmlObject.xmlText(options);
xmlString = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>" + xmlString;
assertTrue(xmlString.contains("<root>"));
assertTrue(xmlString.contains("This is child element 1"));
assertTrue(xmlString.contains("This is child element 2"));
} catch (XmlException e) {
e.printStackTrace();
}
}
In the above test method, we parse the document into an XmlObject using XmlOptions to customize output formatting such as pretty-printing, namespaces, etc. Moreover, asserts are done to establish that a resulting XML string contains an XML declaration and particular XML elements and element contents.
在上述测试方法中,我们使用 XmlOptions 将文档解析为 XmlObject 以自定义输出格式,如漂亮打印、命名空间等。此外,我们还使用 asserts 来确定生成的 XML 字符串是否包含 XML 声明以及特定的 XML 元素和元素内容。
5. Conclusion
5.结论
In this tutorial, we discuss how to convert an XML Document object to a string in Java.
在本教程中,我们将讨论如何在 Java 中将 XML Document 对象转换为字符串。
As always, the complete code samples for this article can be found over on GitHub.
与往常一样,本文的完整代码示例可在 GitHub 上找到。