There are of course a lot of good solutions based on what you need. If it is just configuration, you should have a look at Jakarta commons-configuration and commons-digester.

You could always use the standard JDK method of getting a document :

import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;

[...]

File file = new File("some/path");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(file);
Answer from Guillaume on Stack Overflow
Top answer
1 of 10
58

There are of course a lot of good solutions based on what you need. If it is just configuration, you should have a look at Jakarta commons-configuration and commons-digester.

You could always use the standard JDK method of getting a document :

import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;

[...]

File file = new File("some/path");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(file);
2 of 10
40

XML Code:

<?xml version="1.0"?>
<company>
    <staff id="1001">
        <firstname>yong</firstname>
        <lastname>mook kim</lastname>
        <nickname>mkyong</nickname>
        <salary>100000</salary>
    </staff>
    <staff id="2001">
        <firstname>low</firstname>
        <lastname>yin fong</lastname>
        <nickname>fong fong</nickname>
        <salary>200000</salary>
    </staff>
</company>

Java Code:

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;

public class ReadXMLFile {

  public static void main(String argv[]) {
    try {
    File fXmlFile = new File("/Users/mkyong/staff.xml");
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document doc = dBuilder.parse(fXmlFile);
    doc.getDocumentElement().normalize();

    System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
    NodeList nList = doc.getElementsByTagName("staff");
    System.out.println("----------------------------");

    for (int temp = 0; temp < nList.getLength(); temp++) {
        Node nNode = nList.item(temp);
        System.out.println("\nCurrent Element :" + nNode.getNodeName());
        if (nNode.getNodeType() == Node.ELEMENT_NODE) {
            Element eElement = (Element) nNode;
            System.out.println("Staff id : "
                               + eElement.getAttribute("id"));
            System.out.println("First Name : "
                               + eElement.getElementsByTagName("firstname")
                                 .item(0).getTextContent());
            System.out.println("Last Name : "
                               + eElement.getElementsByTagName("lastname")
                                 .item(0).getTextContent());
            System.out.println("Nick Name : "
                               + eElement.getElementsByTagName("nickname")
                                 .item(0).getTextContent());
            System.out.println("Salary : "
                               + eElement.getElementsByTagName("salary")
                                 .item(0).getTextContent());
        }
    }
    } catch (Exception e) {
    e.printStackTrace();
    }
  }
}

Output:

----------------

Root element :company
----------------------------

Current Element :staff
Staff id : 1001
First Name : yong
Last Name : mook kim
Nick Name : mkyong
Salary : 100000

Current Element :staff
Staff id : 2001
First Name : low
Last Name : yin fong
Nick Name : fong fong
Salary : 200000

I recommended you reading this: Normalization in DOM parsing with java - how does it work?

Example source.

๐ŸŒ
Blogger
javarevisited.blogspot.com โ€บ 2011 โ€บ 12 โ€บ parse-xml-file-in-java-example-tutorial.html
How to Parse or Read XML File in Java >> XML Tutorial Example
@Erieze, You can also modify or write data into XML using DOM or SAX parser, better way is to use XML Binding to Java objects for modifying XML documents form Java programming language.
Discussions

How to read this xml file in java (need to be done by using exported,firstName,lastName,...)? - Stack Overflow
check out for the detailed step here (http://theopentutorials.com/examples/java/jaxb/generate-java-class-from-xml-schema-in-eclipse-ide/) More on stackoverflow.com
๐ŸŒ stackoverflow.com
Reading XML file content in Java - Stack Overflow
Can you tell me best way to read an XML file in Java with sample code? XML content be like below. More on stackoverflow.com
๐ŸŒ stackoverflow.com
Is there an easy way to read an XML file in Java? - Stack Overflow
I'm fairly new to Java and am writing an app that needs an XML config file. The problem I have is that there doesn't seem to be any easy way to do this, which seems a bit strange. I've looked SAX a... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to read XML file using java - Stack Overflow
I am trying to read in some data from an XML file and having some trouble, the XML I have is as follows: More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
javathinking
javathinking.com โ€บ blog โ€บ how-to-read-and-write-xml-files
How to Read and Write XML Files in Java: The Easiest Way Explained โ€” javathinking.com
Handling XML in Java doesnโ€™t have to be intimidating. We covered two practical methods: DOM Parser: Best for small XML files when you need to manually traverse or manipulate the XML structure.
๐ŸŒ
Initial Commit
initialcommit.com โ€บ blog โ€บ how-to-read-xml-file-in-java
How to read XML file in Java
This tutorial shows how to read and parse an XML file in Java using a DOM parser.
Find elsewhere
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ java-xml-parser
Java XML Parser | DigitalOcean
August 4, 2022 - JDOM Read XML File In this tutorial, we will learn how to read XML file to Object using JDOM XML Parser. JDOM Write XML File In this tutorial we will learn how to write XML file in Java using JDOM. JDOM Document provides methods to easily create elements and attributes.
๐ŸŒ
Codoid
codoid.com โ€บ automation-testing โ€บ read-data-from-xml-by-using-different-parsers-in-java
Read Data from XML by Using Different Parsers in Java - Codoid
September 18, 2024 - But the setback that comes with DOM is that it is slow and consumes a large amount of memory because of the way it works. So DOM will be an optimal choice if you are looking to parse a smaller file and not a very large XML file as everything ...
Call ย  1800 (212) 6988
Address ย  TIDEL Park, 305, 3rd Floor, D-North, 4, Rajiv Gandhi Salai, Tharamani,, 600113, Chennai
Top answer
1 of 4
3

I would use JAXB, try this, it works

public class Test1 {
    @XmlAttribute
    String sourceName;
    @XmlAttribute
    String targetName;
    @XmlElement(name = "column")
    List<Test1> columns;

    public static Test1 unmarshal(File file) {
        return JAXB.unmarshal(file, Test1.class);
    }
}
2 of 4
1

You could use Simple form simple XML serialization:

import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;

public class App {

    public static void main(String[] args) throws Exception {
        String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
                + "<table sourceName=\"person\" targetName=\"person\">\n"
                + "    <column sourceName=\"id\" targetName=\"id\"/>\n"
                + "    <column sourceName=\"name\" targetName=\"name\"/>``\n"
                + "</table>";
        Serializer serializer = new Persister();
        Table table = serializer.read(Table.class, xml);
        System.out.println(table.getSourceName());
        System.out.println(table.getTargetName());
        for (Column colunmn : table.getColumns()) {
            System.out.println(colunmn.getSourceName());
            System.out.println(colunmn.getTargetName());
        }
    }
}

Table:

import java.util.List;
import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;

@Root(name = "table")
public class Table {

    @Attribute
    private String sourceName;
    @Attribute
    private String targetName;
    @ElementList(name = "column", inline = true)
    private List<Column> columns;

    public Table() {
    }

    public String getSourceName() {
        return sourceName;
    }

    public void setSourceName(String sourceName) {
        this.sourceName = sourceName;
    }

    public String getTargetName() {
        return targetName;
    }

    public void setTargetName(String targetName) {
        this.targetName = targetName;
    }

    public List<Column> getColumns() {
        return columns;
    }

    public void setColumns(List<Column> columns) {
        this.columns = columns;
    }
}

Column:

import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.Root;

@Root(name = "column")
public class Column {

    @Attribute
    private String sourceName;
    @Attribute
    private String targetName;

    public Column() {
    }

    public String getSourceName() {
        return sourceName;
    }

    public void setSourceName(String sourceName) {
        this.sourceName = sourceName;
    }

    public String getTargetName() {
        return targetName;
    }

    public void setTargetName(String targetName) {
        this.targetName = targetName;
    }
}
๐ŸŒ
GitHub
github.com โ€บ RameshMF โ€บ java-xml-tutorial
GitHub - RameshMF/java-xml-tutorial: Tutorial to parse or processing xml file in Java with different XML Parsers ยท GitHub
DOM Parser is the easiest Java XML parser to learn. DOM parser loads the XML file into memory and we can traverse it node by node to parse the XML. DOM Parser is good for small files but when file size increases it performs slow and consumes ...
Starred by 4 users
Forked by 3 users
๐ŸŒ
How to do in Java
howtodoinjava.com โ€บ home โ€บ java xml โ€บ read an xml file using dom parser in java
Read an XML File using DOM Parser in Java
March 13, 2023 - Learn to read or parse XML documents into String, write to files and convert to POJO using Java DOM parser API with examples.
๐ŸŒ
The Eclipse Foundation
eclipse.org โ€บ forums โ€บ index.php โ€บ t โ€บ 76237
Eclipse Community Forums: Newcomers ยป Why cannot my Java application read my XML file??? | The Eclipse Foundation
The Eclipse Foundation - home to a global community, the Eclipse IDE, Jakarta EE and over 350 open source projects, including runtimes, tools and frameworks.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ read-and-write-xml-files-in-java
How to Read and Write XML Files in Java? - GeeksforGeeks
July 27, 2025 - In the context of XML processing in Java, DOM represents XML document as a tree model, where each node in the tree model corresponds to a part of the document. They are used to work with and navigate this tree using methods of the DOM API. ... Install the JDK (Java Development Kit) installed in your system. Once installed the code into your choice either VSCode, Eclipse, or InteljIdea wherever based on your choice code editor.
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 70744724 โ€บ how-to-read-xml-file-using-java
How to read XML file using java - Stack Overflow
If a XML schema (XSD) or Document Type Definition (DTD) for a given XML is available or can be easily constructed, I prefer to use one of the many libraries to parse XML contents; to name a few StaX, JDOM, DOM4j, JAXB.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ java-program-to-extract-content-from-a-xml-document
Java Program to Extract Content From a XML Document - GeeksforGeeks
July 23, 2025 - Java DOM Parser: DOM stands for Document Object Model. The DOM API provides the classes to read and write an XML file. DOM reads an entire document. It is useful when reading small to medium size XML files. It is a tree-based parser and a little slow when compared to SAX and occupies more space when loaded into memory.
๐ŸŒ
Blogger
javarevisited.blogspot.com โ€บ 2015 โ€บ 07 โ€บ how-to-read-xml-file-as-string-in-java-example.html
How to Read XML File as String in Java? 3 Examples
May 16, 2023 - 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.
๐ŸŒ
Studytonight
studytonight.com โ€บ java-examples โ€บ how-to-read-xml-file-in-java
How to read XML file in Java - Studytonight
In this tutorial we will learn to read an XML file in Java, Java provides an XML parser library that is helpful to read and write the XML document.