XML (eXtensible Markup Language) is widely used for storing and exchanging structured data. In Java, reading and parsing XML files is a fundamental skill for processing and extracting information from XML documents. In this blog post, we will delve into the process of reading and parsing XML files using Java.
Parsing XML Files
The demonstrated Java program reads and parses an XML file using the DOM (Document Object Model) parser. The XML file is specified by the filename passed as an argument to the XMLParser constructor. The XMLParser class initializes the parser and creates a Document object representing the parsed XML file.
class XMLParser {
private Document document;
public XMLParser(String filename, boolean validating) throws IOException, ParserConfigurationException, SAXException {
this.init(filename, validating);
}
public XMLParser(String filename) throws IOException, ParserConfigurationException, SAXException {
this(filename, false);
}
protected void init(String filename, boolean validating) throws IOException, SAXException, ParserConfigurationException {
DocumentBuilderFactory builder = DocumentBuilderFactory.newInstance();
builder.setValidating(validating);
DocumentBuilder documentBuilder = builder.newDocumentBuilder();
this.document = documentBuilder.parse(filename);
}
public Document getDocument() {
return this.document;
}
}
Reading XML Files
The main method of the Main class then retrieves and prints the version, encoding, and standalone status of the XML file using methods provided by the Document class.
The program then retrieves all the “user” elements in the XML file using the getElementsByTagName method of the Document class. It then loops through each user element and retrieves all its child nodes, which are assumed to be attributes of the user. The program then prints out the name and value of each attribute to the console, ignoring any white space nodes. Finally, the program prints a separator line between each user element.
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import javax.xml.parsers.*;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
try {
XMLParser parser = new XMLParser("data.xml");
Document document = parser.getDocument();
/* gets xml file version */
System.out.printf(" XML Version: %s%n", document.getXmlVersion());
/* gets xml file encoding */
System.out.printf(" XML Encoding: %s%n", document.getXmlEncoding());
/* gets whether xml file is a standalone file or not */
System.out.printf("XML Standalone: %b%n", document.getXmlStandalone());
/* gets all the users */
NodeList users = document.getElementsByTagName("user");
/* prints all the users, and associated data out to the screen */
for (int userIndex = 0, userLen = users.getLength(); userIndex < userLen; ++userIndex) {
NodeList userAttributes = users.item(userIndex).getChildNodes();
System.out.println();
for (int attributeIndex = 0, attrLen = userAttributes.getLength(); attributeIndex < attrLen; ++attributeIndex) {
Node attribute = userAttributes.item(attributeIndex);
/* ignore the white space */
if (attribute.getFirstChild() != null) {
System.out.printf(" %-12s: %s%n", attribute.getNodeName(), attribute.getFirstChild().getNodeValue());
}
}
System.out.println("\n------------------------------");
}
} catch (ParserConfigurationException | IOException | SAXException e) {
System.out.println(e.getMessage());
}
}
}
Conclusion
Overall, this program provides a basic example of how to read and parse an XML file in Java using the DOM parser, and can serve as a starting point for more complex XML parsing applications.
Here is the sample XML file being parsed in this post:
<?xml version="1.0" encoding="UTF-16" standalone="yes" ?>
<!DOCTYPE users>
<users>
<user>
<name>Edward Jnr.</name>
<age>24</age>
<language>Java</language>
</user>
<user>
<name>George</name>
<age>28</age>
<language>PHP</language>
</user>
<user>
<name>Cherish</name>
<age>4</age>
<language>JavaScript</language>
</user>
<user>
<name>Lucia</name>
<age>4</age>
<language>Python</language>
</user>
</users>
I sincerely hope that you find this code helpful. If you wish to learn more about Java, please subscribe to our newsletter today and continue your Java learning journey with us!