How to parse XML using DOM parser using java

Posted By : Tushar Paliwal | 31-Dec-2014

In few projects I found that projects need functionality of fetching data from XML, even I also required the same functionality in one of my project. I investigated over Internet and found a solution which I would like to share with you through this blog.

This functionality can be implemented using Java API with jaxp-api-1.4.2.jar. Here I would show you an example, how does it work. In my example I'll use DOM parser.

DOM parser : This parser loads complete XML in to memory and then iterate data using NodeList and node by node.

Below are the steps which will describe my example :

Step 1 :

In this step I'll create a model of student, In which toString() method is override.

class Student{
		String id;
		String firstName;
		String lastName;

		@Override
		public String toString() {
			return firstName+" "+lastName;
		}
	}

 

Step 2 :

In this step parser will load complete XML file in memory.

//Get the DOM Builder Factory
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

//Get the DOM Builder
DocumentBuilder builder = factory.newDocumentBuilder();

//Load and Parse the XML document
//document contains the complete XML as a Tree.
Document document = builder.parse(new File("/xml/student.xml"));



Step 3 :

In this step data of XML file is being set to Student model, which can be stored in the database or process for further use.

	List studentList = new ArrayList<>();
		//Iterating through the nodes and extracting the data.
		NodeList nodeList = document.getDocumentElement().getChildNodes();
		for (int i = 0; i < nodeList.getLength(); i++) {
			//We have encountered an  tag.
			Node node = nodeList.item(i);
			if (node instanceof Element) {
				Student student = new Student();
				student.id = node.getAttributes().getNamedItem("id").getNodeValue();
				NodeList childNodes = node.getChildNodes();
				for (int j = 0; j < childNodes.getLength(); j++) {
					Node cNode = childNodes.item(j);
					//Identifying the child tag of student encountered.
					if (cNode instanceof Element) {
						String content = cNode.getLastChild().getTextContent().trim();
						switch (cNode.getNodeName()) {
							case "firstName":
								student.firstName = content;
								break;
							case "lastName":
								student.lastName = content;
								break;
						}
					}
				}
				studentList.add(student);
			}

 

Step 4 :

Below is the input XML data which is used in this example.


  
    John
    Carter
  
  
    Adam
    Davis
  
  
    Maak
    Cruise
  

Now we can either process data for further use or can store data in to database which is being stored in the student list.

I hope this blog will help you. Kindly feel free to ask any query.

 

About Author

Author Image
Tushar Paliwal

Tushar is a developer with experience in Groovy and Grails , Spring and enterprise Java Technologies.

Request for Proposal

Name is required

Comment is required

Sending message..