Java Integration
Transform Anything with Java
Reading XML using DOM Parser
1. To start with we will create a project named DOMReader
2. We will start with two simple java class files. readXMLDOM and DOMReader. We will update the code step by step.
package com.javatweaks.dom;
import java.util.ArrayList;
import java.util.List;
import com.javatweaks.sax.read.Items;
public class DOMReader {
public List<Items> getDataFromXML(String filename) {
List<Items> data = new ArrayList<>();
return data;
}
}
package com.javatweaks.dom;
import java.io.IOException;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
import com.javatweaks.sax.read.Items;
import com.javatweaks.sax.read.SAXItemsHandler;
public class readXMLDOM {
public static void main(String[] args) throws IOException, ParserConfigurationException {
DOMReader reader = new DOMReader();
String filename = "C:\\Users\\xxxx\\Documents\\Java_XML\\items.xml";
List<Items> data = reader.getDataFromXML(filename);
System.out.println("There are " + data.size() + " records");
}
}
3. Reading the XML file is similar to what we did to write XML.
-
Create a DocumentBuilderFactory object.
-
Create a DocumentBuilder using the factory instance.
-
Parse the xml document using builder instance.
File inputFile = new File(filename);
Document doc = null;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
doc = builder.parse(inputFile);
4. Place the code inside the try catch block, because later we will handle exceptions while reading the XML File.
5. The simplest way to retrieve data is using a NodeList and then getting each element using the below code.
NodeList list = doc.getElementsByTagName("item");
for (int i = 0; i < list.getLength(); i++) {
Items item = new Items();
data.add(item);
Element itemElement = (Element) list.item(i);
String idString = itemElement.getAttribute(Items.ID);
item.setId(Integer.parseInt(idString));
String content = getTextFromElement(itemElement, Items.MAN);
item.setManufacturer(content);
content = getTextFromElement(itemElement, Items.NAME);
item.setName(content);
content = getTextFromElement(itemElement, Items.PRICE);
item.setPrice(content);
}
public String getTextFromElement(Element itemElement, String elementName) {
Element node = (Element) itemElement.getElementsByTagName(elementName).item(0);
String content = node.getTextContent();
return content;
}
Below is the code.
DOMReader.java
package com.javatweaks.dom;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class DOMReader {
public List<Items> getDataFromXML(String filename) {
List<Items> data = new ArrayList<>();
File inputFile = new File(filename);
Document doc = null;
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
doc = builder.parse(inputFile);
} catch (ParserConfigurationException | SAXException | IOException e) {
e.printStackTrace();
}
NodeList list = doc.getElementsByTagName("item");
for (int i = 0; i < list.getLength(); i++) {
Items item = new Items();
data.add(item);
Element itemElement = (Element) list.item(i);
String idString = itemElement.getAttribute(Items.ID);
item.setId(Integer.parseInt(idString));
String content = getTextFromElement(itemElement, Items.MAN);
item.setManufacturer(content);
content = getTextFromElement(itemElement, Items.NAME);
item.setName(content);
content = getTextFromElement(itemElement, Items.PRICE);
item.setPrice(content);
}
return data;
}
public String getTextFromElement(Element itemElement, String elementName) {
Element node = (Element) itemElement.getElementsByTagName(elementName).item(0);
String content = node.getTextContent();
return content;
}
}
readXMLDOM.java
package com.javatweaks.dom;
import java.io.IOException;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
import com.javatweaks.sax.read.Items;
public class readXMLDOM {
public static void main(String[] args) throws IOException, ParserConfigurationException {
DOMReader reader = new DOMReader();
String filename = "C:\\Users\\xxxx\\Documents\\Java_XML\\items.xml";
List<Items> data = reader.getDataFromXML(filename);
for (Items items : data) {
System.out.println(items.getManufacturer()+" "+ items.getId() + " "+ items.getName() +" " + items.getPrice());
}
}
}
6. If we run the program, the output will be like below.
Apple Inc 1 iPhone 5 600
Apple Inc 2 iPhone 6 700
Motorola 3 Moto X 200
Motorola 4 Moto G 450
Samsung Corp 5 Samsung S4 550