Java XML Parsing Tutorial: DOM - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Community



As the whole document is stored in memory at once, it is preferred for only small documents.
  • It is a tree model parser (Object-based).
  • First, it loads the file into the memory and then parses the file.
  • It has memory constraints since it loads the whole XML file before parsing.
  • It supports both read and write operations.
  • Backward and forward searches are possible for searching the tags and evaluating the information inside the tags.
  • It is slower at run time.

Content of Employee.java
package com.t4b.parser.dom.test; public class Employee { private String name; private int age; private int id; private String type; public Employee(String name, int id, int age, String type) { this.name = name; this.age = age; this.id = id; this.type = type; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String toString() { StringBuffer sb = new StringBuffer(); sb.append("Employee Details - "); sb.append("Name:" + getName()); sb.append(", "); sb.append("Type:" + getType()); sb.append(", "); sb.append("Id:" + getId()); sb.append(", "); sb.append("Age:" + getAge()); sb.append("."); return sb.toString(); } }

Content of DomParserTest.java
package com.t4b.parser.dom.test; import java.io.IOException; import java.util.ArrayList; import java.util.Iterator; 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 DomParserTest { List<Employee> myEmpls; Document dom; public DomParserTest() { myEmpls = new ArrayList<Employee>(); } public void runExample(String filename) { parseXmlFile(filename); parseDocument(); printData(); } private void parseXmlFile(String filename) { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); try { DocumentBuilder db = dbf.newDocumentBuilder(); dom = db.parse(filename); } catch (ParserConfigurationException pce) { pce.printStackTrace(); } catch (SAXException se) { se.printStackTrace(); } catch (IOException ioe) { ioe.printStackTrace(); } } private void parseDocument() { Element docEle = dom.getDocumentElement(); NodeList nl = docEle.getElementsByTagName("Employee"); if (nl != null && nl.getLength() > 0) { for (int i = 0; i < nl.getLength(); i++) { Element el = (Element) nl.item(i); Employee e = getEmployee(el); myEmpls.add(e); } } } private Employee getEmployee(Element empEl) { String name = getTextValue(empEl, "Name"); int id = getIntValue(empEl, "Id"); int age = getIntValue(empEl, "Age"); String type = empEl.getAttribute("type"); Employee e = new Employee(name, id, age, type); return e; } private String getTextValue(Element ele, String tagName) { String textVal = null; NodeList nl = ele.getElementsByTagName(tagName); if (nl != null && nl.getLength() > 0) { Element el = (Element) nl.item(0); textVal = el.getFirstChild().getNodeValue(); } return textVal; } private int getIntValue(Element ele, String tagName) { return Integer.parseInt(getTextValue(ele, tagName)); } private void printData() { System.out.println("No of Employees '" + myEmpls.size() + "'."); Iterator<Employee> it = myEmpls.iterator(); while (it.hasNext()) { System.out.println(it.next().toString()); } } public static void main(String[] args) { DomParserTest dpt = new DomParserTest(); dpt.runExample("employees.xml"); } }

Content of employees.xml document for parsing
<?xml version="1.0" encoding="UTF-8"?> <Personnel> <Employee type="permanent"> <Name>John</Name> <Id>1000</Id> <Age>34</Age> </Employee> <Employee type="contract"> <Name>Robin</Name> <Id>1001</Id> <Age>25</Age> </Employee> <Employee type="permanent"> <Name>Neil</Name> <Id>1002</Id> <Age>26</Age> </Employee> </Personnel>




Happy Exploring!

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.