Convert Java org.w3c.dom.Document objects and XML strings

Sample code

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();
  }
}

Sample execution result

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>

Reference material

Recommended Posts

Convert Java org.w3c.dom.Document objects and XML strings
[Java] Convert character strings to uppercase / lowercase (AOJ⑨-swap uppercase and lowercase)
Manipulating Java strings
Kantai Collection Java # 1 Classes and Objects [For Beginners]
Java and JavaScript
XXE and Java
[Java] Convert and import file values with OpenCSV
<java> Read Zip file and convert directly to string
Convert Java enum enums and JSON to and from Jackson
[Java] Sorting tips when strings and numbers are mixed
How to convert A to a and a to A using AND and OR in Java
Reverse Enum constants from strings and values in Java
[Java] [XML signature] Add prefix and ID to Signature tag
[Java] Convert JSON to Java and Java to JSON-How to use GSON and Jackson-
Mutual conversion between Java objects and JSON using Moshi
[Java] Handling of character strings (String class and StringBuilder class)
[Java] Convert PDF version
Getters and setters (Java)
java classes, instances, objects
[Java] Thread and Runnable
Java true and false
[Java] String comparison and && and ||
Format XML in Java
mutable and immutable objects
Java --Serialization and Deserialization
[Java] Arguments and parameters
timedatectl and Java TimeZone
[Java] Branch and repeat
[Java] Variables and types
java (classes and instances)
[Java] Overload and override
[Java] Comparison method of character strings and comparison method using regular expressions
Get attributes and values from an XML file in Java
Differences in how to handle strings between Java and Perl