Tuesday, May 16, 2023

How to Read XML File as String in Java? 3 Examples

Suppose you have an XML file and you just want to read and display the whole file as String in Java, may be for debugging purposes. If you are wondering how to do that in Java, well there are many ways to read XML as String in Java. You can do it in one line if you are fine with using an open-source library or you can do it in a couple of lines of code in core Java as well. Since an XML file is also a file, you can use BufferedReader or FileInputStream to read the content of an XML file as String by using the techniques, I have discussed in my earlier post 3 ways to convert InputStream to String in Java

But this post is about a new library called jcabi-xml which makes working with XML file really easy. You can parse the XML file using XPath expression, you can do XSL transformation, XSD schema validation and you can even parse whole XML file as String in just a couple of lines of code.

I was playing with this new XML library and thought to share a couple of examples of XML processing to show how powerful it is. By the way, while reading the XML file as String one thing you must remember is character encoding, which is specified in the header of an XML file.

If you use any XML library or even XML parser like DOM and SAX, you don't need to make any additional adjustment, they will take care of reading String incorrect encoding, but if you use BufferedReader or any general Stream reader, you must ensure that correct character encoding is used.

In this article, you will learn three ways to read XML files as String in Java, first by using FileReader and BufferedReader, second by using DOM parser, and third by using open-source XML library jcabi-xml.




3 Ways to Read XML into String in Java

There are multiple ways to read and process XML files and generate String out of it and we will see three of them. Two of those approaches are based on standard classes and interfaces from JDK itself and 3rd approach will make use of an open-source library. For our example purpose, we will read the following XML file as String :
<?xml version="1.0" encoding="UTF-8"?>
<banks>
    <bank id="1">
        <name>Barclays Bank</name>
        <headquarter>London</headquarter>
    </bank>
    <bank id="2">
        <name>Goldman Sachs</name>
        <headquarter>NewYork</headquarter>
    </bank>
    <bank id="3">
        <name>ICBC</name>
        <headquarter>Beijing</headquarter>
    </bank>
</banks>

BTW, if you are new to XML processing in Java, I also suggest you take a look at Core Java Volume II - Advanced Features, 9th Edition by Cay S. Horstmann. It will help you to learn and understand more advanced features of Java e.g. JDBC, XML, JMX, JMS, etc.

How to Read XML File as String in Java? 3 Examples





Java Program to read XML into String

Here is our Java program to read XML as String. It contains three examples, first, one uses BufferedReader and reads XML like a text file. It's not great because you need to specify character encoding by yourself while the XML parser can read it directly from XML header. In this approach, you can build XML string by reading file line by line.

The second example is about the DOM parser, which is great for reading small XML files because it load them entirely into memory and then parse it by creating a DOM tree. It's good for parsing huge XML file because that would require large memory and still may end up in java.lang.OutOfMemoryError : Java heap space.  

An interesting point is, toString() method of Document object will not return String representation of XML, which means this is not suitable for the job in hand but you can do a lot by calling various methods of DOM API.

The third example is interesting, it uses an open source library called jcabi-xml, which provides a convenient class called XMLDocument to represent an XML file in memory. This class has overridden the toString() method to return String representation of XML file itself. So you can read entire XML as String by using just two lines.

How to read XML as String in Java 3 Example


Here is our sample Java program to demonstrate all three methods :

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import com.jcabi.xml.XML;
import com.jcabi.xml.XMLDocument;


/**
  * Java Program to read XML as String using BufferedReader,
  *  DOM parser and jCabi-xml 
  * open source library.
  */
public class XmlAsStringInJava {

    public static void main(String[] args) 
          throws ParserConfigurationException, SAXException, IOException {
        
        // our XML file for this example
        File xmlFile = new File("info.xml");
        
        // Let's get XML file as String using BufferedReader
        // FileReader uses platform's default character encoding
        // if you need to specify a different encoding, 
        // use InputStreamReader
        Reader fileReader = new FileReader(xmlFile);
        BufferedReader bufReader = new BufferedReader(fileReader);
        
        StringBuilder sb = new StringBuilder();
        String line = bufReader.readLine();
        while( line != null){
            sb.append(line).append("\n");
            line = bufReader.readLine();
        }
        String xml2String = sb.toString();
        System.out.println("XML to String using BufferedReader : ");
        System.out.println(xml2String);
        
        bufReader.close();
       
        // parsing XML file to get as String using DOM Parser
        DocumentBuilderFactory dbFactory 
             = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = dbFactory.newDocumentBuilder();
        Document xmlDom = docBuilder.parse(xmlFile);
        
        String xmlAsString = xmlDom.toString(); 
        // this will not print what you want
        System.out.println("XML as String using DOM Parser : ");
        System.out.println(xmlAsString);
        
        
        // Reading XML as String using jCabi library
        XML xml = new XMLDocument(new File("info.xml"));
        String xmlString = xml.toString();        
        System.out.println("XML as String using JCabi library : " );
        System.out.println(xmlString);
      }
}


Output
XML to String using BufferedReader :
<?xml version="1.0" encoding="UTF-8"?>
<banks>
    <bank id="1">
        <name>Barclays Bank</name>
        <headquarter>London</headquarter>
    </bank>
    <bank id="2">
        <name>Goldman Sachs</name>
        <headquarter>NewYork</headquarter>
    </bank>
    <bank id="3">
        <name>ICBC</name>
        <headquarter>Beijing</headquarter>
    </bank>
</banks>


XML as String using DOM Parser :
[#document: null]

XML as String using JCabi library :
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<banks>
    <bank id="1">
        <name>Barclays Bank</name>
        <headquarter>London</headquarter>
    </bank>
    <bank id="2">
        <name>Goldman Sachs</name>
        <headquarter>NewYork</headquarter>
    </bank>
    <bank id="3">
        <name>ICBC</name>
        <headquarter>Beijing</headquarter>
    </bank>
</banks>


That's all about how to read XML file as String in Java. You have seen all three approaches to get XML as String and you can compare them easily. BufferedReader doesn't take XML encoding defined in the header, you have to specify it manually if you want to read an XML file encoded in a different character encoding.


In our example, since we use FileReader, we don't have that option, but if you need to specify a different encoding the platform's default character encoding then please use InputStreamReader. Also, when we use BufferedReader you also need to take are of the new line, which could be different in different platform e.g. UNIX and Windows uses different characters for the new line.

DOM parser is the right way to parse XML files but unfortunately, its toString() doesn't return what you want. Now if you look at our two line code using new XML library jcabi-xml, its a breeze. That's why if you can use the open-source library, use this one, it will make your XML parsing, validation, and transformation easy.


If you like this Java XML tutorial and are interested to learn more about XML processing in Java, you can also check out the following tutorials :
  • How to convert Java object to XML document using JAXB? [example]
  • Java guide to parse XML file using DOM parser? [example]
  • Step by Step guide to read XML using SAX parser in Java? [guide]
  • How to select XML element using XPath in Java? [solution]
  • A difference between DOM and SAX parser in XML? [answer]
  • How to escape XML special characters in Java String? [answer]
  • How to marshall and unmarshall Java to XML using JAXB? [example]
  • XPath Tutorials for Beginner Java Developers [tutorial]
  • How to process XML file using JDOM parser in Java? [example]
  • How to evaluate XPath expression in Java? [example]

1 comment:

  1. java.lang.ClassNotFoundException: com.jcabi.immutable.ArrayMap
    I'm getting this error using jcabi library.

    ReplyDelete