Save the following sample code with the file name MyApp.java.
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;
/**
*Sample code for converting XML strings to Document objects(Character encoding is UTF-8 fixed)。
*/
public class MyApp {
/**
*Converts an XML string to a Document object.
* @param xml XML string
* @return Document object
* @throws ParserConfigurationException
* @throws SAXException
* @throws IOException
*/
public static Document toDocument(String xml) throws ParserConfigurationException, SAXException, IOException {
try (StringReader reader = new StringReader(xml)) {
InputSource source = new InputSource(reader);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
return builder.parse(source);
}
}
/**
*Converts a Document object to an XML string.
* @param doc Document object
* @return XML string
* @throws IOException
* @throws TransformerException
*/
public static String toString(Document doc) throws IOException, TransformerException {
try (ByteArrayOutputStream output = new ByteArrayOutputStream()) {
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
//Default is UTF even if not specified-8
transformer.setOutputProperty(OutputKeys.ENCODING, StandardCharsets.UTF_8.name());
DOMSource source = new DOMSource(doc);
StreamResult result = new StreamResult(output);
transformer.transform(source, result);
return output.toString(StandardCharsets.UTF_8);
}
}
//Operation check method
public static void main(String[] args) throws IOException, SAXException, ParserConfigurationException, TransformerException, XPathExpressionException {
//Prepare XML character string for operation check
//Use string literals with Text Blocks feature available from Java 15
String xml = """
<?xml version="1.0" encoding="UTF-8"?>
<feed xml:lang="ja-JP" xmlns="http://www.w3.org/2005/Atom">
<id>helloworldfeed</id>
<author>niwasawa</author>
<title>Hello World feed</title>
<updated>2000-08-06T00:00:00+09:00</updated>
<entry>
<id>helloworldfeed:helloworld1</id>
<title>Hello World Hello World</title>
<content>Hello World Hello World</content>
<updated>2000-08-06T00:00:00+09:00</updated>
</entry>
</feed>
""";
//Convert XML string to Document object
System.out.println("*****Convert XML string to Document object*****");
Document doc = toDocument(xml);
System.out.println("title: " + XPathFactory.newInstance().newXPath().evaluate("//title", doc, XPathConstants.STRING));
System.out.println();
//Convert Document object to XML string
System.out.println("*****Convert Document object to XML string*****");
doc.setXmlStandalone(true);
String outputXml = toString(doc);
System.out.println(outputXml);
System.out.println();
}
}
Execution environment: Java 15 (Oracle JDK) + macOS Catalina
$ java MyApp.java
*****Convert XML string to Document object*****
title:Hello World feed
*****Convert Document object to XML string*****
<?xml version="1.0" encoding="UTF-8"?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ja-JP">
<id>helloworldfeed</id>
<author>niwasawa</author>
<title>Hello World feed</title>
<updated>2000-08-06T00:00:00+09:00</updated>
<entry>
<id>helloworldfeed:helloworld1</id>
<title>Hello World Hello World</title>
<content>Hello World Hello World</content>
<updated>2000-08-06T00:00:00+09:00</updated>
</entry>
</feed>
Recommended Posts