x Java Java 8 JUnit JSON
  • XML
  • JDBC Spring Boot Microservices React Contact Us

    How to parse and create XML file using JDOM2 parser in Java ?


    JDOM parser uses similar approach as DOM parser but is easier to use. JDOM used proprietary Apache APIs, so it tie you to a specific implementation that can evolve in time or lose backwards compatibility.


    Parse XML file using JDOM parser

    First you need to download jdom-2.0.6.jar from JDOM 2.0.6

    XML file to parse: employees.xml
    <?xml version = "1.0"?>    
    <employees>
        <employee id="123">
            <firstname>John</firstname>
            <lastname>Carter</lastname>
        </employee>
        <employee id="124">
            <firstname>Jay</firstname>
            <lastname>Joseph</lastname>
        </employee>
        <employee id="125">
            <firstname>James</firstname>
            <lastname>Hogg</lastname>
        </employee>
    </employees>
    	
    //JDOM2 parser
    import java.io.File;
    import java.io.IOException;
    import java.util.List;
    
    import org.jdom2.Document;
    import org.jdom2.Element;
    import org.jdom2.JDOMException;
    import org.jdom2.input.SAXBuilder;
    
    public class TestJDOM2 {
        public static void main(String[] args) {
           
            SAXBuilder builder = new SAXBuilder();
            File xmlFile = new File("c:\\employees.xml");  
            
            try {
    
                Document document = (Document) builder.build(xmlFile);
                Element rootNode = document.getRootElement();
                System.out.println("Root: " + rootNode.getName());
                
                List<Element> list = rootNode.getChildren("employee");
            
                for (int i = 0; i < list.size(); i++) {
                    Element node = (Element) list.get(i);
                    System.out.println("id : " + node.getAttributeValue("id"));
                    System.out.println("First Name : " + node.getChildText("firstname"));
                    System.out.println("Last Name : " + node.getChildText("lastname"));
                }
                
            } catch (IOException  e) {
             e.printStackTrace();
            } catch (JDOMException  e) {
             e.printStackTrace();
            } 
        }
    }
    	

    Output :

    Root: employees
    id : 123
    First Name : John
    Last Name : Carter
    id : 124
    First Name : Jay
    Last Name : Joseph
    id : 125
    First Name : James
    Last Name : Hogg
    	


    Create XML using JDOM2

    JDOM parser uses similar approach as DOM parser but is easier to use. JDOM used proprietary Apache APIs, so it tie you to a specific implementation that can evolve in time or lose backwards compatibility.

    You can create XML file using JDOM parser as following:


    XML file to create: employees.xml
    <?xml version = "1.0"?>    
    <employees>
        <employee id="1">
            <firstname>Mark</firstname>
            <lastname>Carter</lastname>
        </employee>
        <employee id="2">
            <firstname>James</firstname>
            <lastname>Joseph</lastname>
        </employee>
        <employee id="3">
            <firstname>John</firstname>
            <lastname>Hogg</lastname>
        </employee>
    </employees>
    	
    //JDOM2 parser
    import java.io.FileWriter;
    import java.io.IOException;
    
    import org.jdom2.Attribute;
    import org.jdom2.Document;
    import org.jdom2.Element;
    import org.jdom2.output.Format;
    import org.jdom2.output.XMLOutputter;
    
    public class JDOM2FileWriter {
        public static void main(String[] args) {
           
            try {
                Element employees = new Element("employees");
                Document doc = new Document(employees);
    
                Element employee = new Element("employee");
                employee.setAttribute(new Attribute("id", "1"));
                employee.addContent(new Element("firstname").setText("Mark"));
                employee.addContent(new Element("lastname").setText("Carter"));
    
                doc.getRootElement().addContent(employee);
    
                Element employee2 = new Element("employee");
                employee2.setAttribute(new Attribute("id", "2"));
                employee2.addContent(new Element("firstname").setText("James"));
                employee2.addContent(new Element("lastname").setText("Joseph"));
      		
                doc.getRootElement().addContent(employee2);
      		
                Element employee3 = new Element("employee");
                employee3.setAttribute(new Attribute("id", "3"));
                employee3.addContent(new Element("firstname").setText("John"));
                employee3.addContent(new Element("lastname").setText("Hogg"));
    
                doc.getRootElement().addContent(employee3);
    
                XMLOutputter xmlOutput = new XMLOutputter();
    
                xmlOutput.setFormat(Format.getPrettyFormat());
                xmlOutput.output(doc, new FileWriter("c:\\employees.xml"));
                
            } catch (IOException  e) {
                e.printStackTrace();
            } 
        }
    }
    	

    References :

    JDOM Introduction

    DOM Parser Example