Showing posts with label DOM and SAX Parser. Show all posts
Showing posts with label DOM and SAX Parser. Show all posts

Update or Edit XML File using DOM Parser


In our earlier tutorial we have seen how to read XML file using DOM and SAX Parser. In this tutorial we will see how to edit or update XMl file using DOM parser. We have used below sample XML to update using DOM parser.

SAMPLE XML:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?> 
<organization>
 <employee mode="permanent">
  <name>John</name>
  <empid>1234</empid>
  <designation>SE</designation>
  <technology>Java</technology>
 </employee>
 <employee mode="contract">
  <name>David</name>
  <empid>4545</empid>
  <designation>Manager</designation>
  <technology>.NET</technology>
 </employee>
</organization>


XML AFTER UPDATE: 

<?xml version="1.0" encoding="UTF-8" standalone="no" ?> 
<organization>
 <employee mode="permanent">
  <name>John</name> 
  <empid>1234</empid> 
  <designation>SE</designation> 
  <age>35</age> 
 </employee>
 <employee mode="permanent">
  <name>David</name> 
  <empid>4545</empid> 
  <designation>Senior Manager</designation> 
  <age>35</age> 
 </employee>
 <employee mode="permanent">
  <name>Steve</name> 
  <empid>5635</empid> 
  <designation>Lab Engineer</designation> 
  <age>38</age> 
 </employee>
</organization>


If we seen above XML we have modified such as
  • Deleting <technology> node from the XML
  • Updating attribute value from "contract" to "permanent" for 2nd employee
  • Updating <designation> node value from "Manager" to "Senior Manager"
  • Adding new <employee> element


Below example java code will be used to modify the XML as per given above. 


import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Attr;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class EditXML {
 
 public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, TransformerException {
  editMyXML("D:\\document.xml");
 }
 
 public static void editMyXML(String file) throws ParserConfigurationException, SAXException, IOException, TransformerException{
  
  DocumentBuilderFactory builderFac = DocumentBuilderFactory.newInstance();
  DocumentBuilder builder = builderFac.newDocumentBuilder();
     Document doc = builder.parse(new File(file));

     System.out.println ("ROOT: " + doc.getDocumentElement().getNodeName());
        NodeList list = doc.getElementsByTagName("employee");
        System.out.println("No. Of Employees: " + list.getLength());
                
        for(int i= 0; i<list.getLength(); i++){
            Node node = list.item(i);
            if(node.getNodeType() == Node.ELEMENT_NODE) {
                Element element = (Element)node;
                String mode, name, empid, designation, technology;
                                
                mode = element.getAttribute("mode");
                
                NodeList nListName = element.getElementsByTagName("name");
                Element nElementName = (Element)nListName.item(0);
                NodeList tListName = nElementName.getChildNodes();
                name =((Node)tListName.item(0)).getNodeValue().trim();
                
                NodeList nListEmpId = element.getElementsByTagName("empid");
                Element nElementEmpId = (Element)nListEmpId.item(0);
                NodeList tListEmpId = nElementEmpId.getChildNodes();
                empid = ((Node)tListEmpId.item(0)).getNodeValue().trim();
                
                NodeList nListDesi = element.getElementsByTagName("designation");
                Element nElementDesi = (Element)nListDesi.item(0);
                NodeList tListDesi = nElementDesi.getChildNodes();
                designation = ((Node)tListDesi.item(0)).getNodeValue().trim();
                
                NodeList nListTech = element.getElementsByTagName("technology");
                Element nElementTech = (Element)nListTech.item(0);
                NodeList tListTech = nElementTech.getChildNodes();
                technology =((Node)tListTech.item(0)).getNodeValue().trim();
                
                // Updating only for 4545 (David) Employee
                if(empid.equals("4545")){
                 
                 // Updating MODE from "contract" to "Permanent"
                 node.getAttributes().getNamedItem("mode").setTextContent("permanent");
                 
                 // Updating Designation from "Manager" to "Senior Manager"
                 ((Node)tListDesi.item(0)).setTextContent("Senior Manager");
                }
                
                // Adding new Node called "age"
                Element nElementAge = doc.createElement("age");
                nElementAge.appendChild(doc.createTextNode("35"));
                element.appendChild(nElementAge);
                
                // Deleting "technology" Node
                element.removeChild(nElementTech);
            }
        }
        
        //Adding new employee
        addNewEmployee(doc, "permanent", "Steve", "5635", "Lab Engineer", "38");
        
        
        // Writing update XML file
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
  Transformer transformer = transformerFactory.newTransformer();
  DOMSource source = new DOMSource(doc);
  StreamResult result = new StreamResult(new File("D:\\document.xml"));
  transformer.transform(source, result);
 }
 
 public static void addNewEmployee(Document doc, String mode, String name, String empId, String designation, String age){
  
  Element root = doc.getDocumentElement();
        
        Element element = doc.createElement("employee");
        root.appendChild(element);
        
  Attr attr = doc.createAttribute("mode");
  attr.setValue("permanent");
  element.setAttributeNode(attr);
  
  Element nElementName = doc.createElement("name");
        nElementName.appendChild(doc.createTextNode("Steve"));
        element.appendChild(nElementName);
        
        Element nElementEmpId = doc.createElement("empid");
        nElementEmpId.appendChild(doc.createTextNode("5635"));
        element.appendChild(nElementEmpId);
        
        Element nElementDesi = doc.createElement("designation");
        nElementDesi.appendChild(doc.createTextNode("Lab Engineer"));
        element.appendChild(nElementDesi);
  
  Element nElementAge = doc.createElement("age");
        nElementAge.appendChild(doc.createTextNode("38"));
        element.appendChild(nElementAge);
  } 
}








DOM and SAX Parser


In this tutorial we will see about DOM and SAX parser along with simple Java code to parse the XML. 

Common XML document which we are going to use for both DOM and SAX parser are 


SAMPLE XML:
<?xml version="1.0" encoding="UTF-8"?>
<organization>
 <employee mode="permanent">
  <name>John</name>
  <empid>1234</empid>
  <designation>SE</designation>
  <technology>Java</technology>
 </employee>
 <employee mode="contract">
  <name>David</name>
  <empid>4545</empid>
  <designation>Manager</designation>
  <technology>.NET</technology>
 </employee>
</organization>


DOM Parser:

Document Object Model (DOM) parser will creates a complete tree structure in memory from the XML provided and reads each node values as and when required. 

The advantage of DOM parser is provided with lot of rich functionality where developers can make use without any additional coding. Also when the document loaded into memory developers can access any part of the DOM tree and can modify.

Disadvantages are need more memory in case of huge document in size. 
it takes a little bit longer to learn how to work with it.



DOM Parser for above XML document:



import java.io.File;
import java.io.IOException;
import org.w3c.dom.*;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;

public class DOMParser {

 public static void main(String[] args) {
  String file = "document.xml";
  
  parseXMLUsingDOM(file);
 }
 
 public static void parseXMLUsingDOM(String file){
  
  try {
   DocumentBuilderFactory builderFac = DocumentBuilderFactory.newInstance();
   DocumentBuilder builder = builderFac.newDocumentBuilder();
      Document doc = builder.parse(new File(file));

      System.out.println ("ROOT: " + doc.getDocumentElement().getNodeName());
            NodeList list = doc.getElementsByTagName("employee");
            System.out.println("No. Of Employees: " + list.getLength());
            
            for(int i= 0; i<list.getLength(); i++){
                    Node node = list.item(i);
                    if(node.getNodeType() == Node.ELEMENT_NODE) {
                        Element element = (Element)node;
                        System.out.println("\nEMP MODE: "+element.getAttribute("mode"));
                        
                        NodeList nList = element.getElementsByTagName("name");
                        Element nElement = (Element)nList.item(0);
                        NodeList tList = nElement.getChildNodes();
                        System.out.println("NAME: " + ((Node)tList.item(0)).getNodeValue().trim());
                        
                        nList = element.getElementsByTagName("empid");
                        nElement = (Element)nList.item(0);
                        tList = nElement.getChildNodes();
                        System.out.println("EMP_ID: " + ((Node)tList.item(0)).getNodeValue().trim());
                        
                        nList = element.getElementsByTagName("designation");
                        nElement = (Element)nList.item(0);
                        tList = nElement.getChildNodes();
                        System.out.println("DESIGNATION: " + ((Node)tList.item(0)).getNodeValue().trim());
                        
                        nList = element.getElementsByTagName("technology");
                        nElement = (Element)nList.item(0);
                        tList = nElement.getChildNodes();
                        System.out.println("TECHNOLOGY: " + ((Node)tList.item(0)).getNodeValue().trim());
                    }
            }            
     } catch (ParserConfigurationException e) {
      e.printStackTrace();  
  } catch (SAXException e) {
      e.printStackTrace();
  } catch (IOException e) {
      e.printStackTrace();
  } 
 }
}


OUTPUT:

ROOT: organization
No. Of Employees: 2

EMP MODE: permanent
NAME: John
EMP_ID: 1234
DESIGNATION: SE
TECHNOLOGY: Java

EMP MODE: contract
NAME: David
EMP_ID: 4545
DESIGNATION: Manager
TECHNOLOGY: .NET



SAX Parser:

Simple API for XML (SAX) parser will not create any internal tree structure as like DOM. It just search for the component occurrences as per input and it will gives the values. Always SAX parser will read only specific document values as requested. 

As advantage SAX parser is much more space efficient in case of a huge document, because its not creating complete tree structure like DOM. Next its faster and easy to implement with basic needs. 
From functionality side it serves less as compared to DOM.


SAX Parser for above XML document:

import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class SAXParserDemo {
 
 public static void main(String[] args) {
  String file = "document.xml";
  
  parseXMLUsingSAX(file);
 }
 
 public static void parseXMLUsingSAX(String file) {
  try{
   DefaultHandler handler = createHandler();
   
   SAXParserFactory factory = SAXParserFactory.newInstance();
   SAXParser parser = factory.newSAXParser();
   
   parser.parse(file, handler);
   
  }catch (ParserConfigurationException  e) {
   e.printStackTrace();
  }catch (SAXException e) {
   e.printStackTrace();
  }catch (IOException e) {
   e.printStackTrace();
  }
 }
 
 public static DefaultHandler createHandler(){
  DefaultHandler handler = null;
  try{
   handler = new DefaultHandler(){
    boolean fName = false;
    boolean fEmpId = false;
    boolean fDesig = false;
    boolean fTech = false;
    public void startElement(String uri, String vName, String tagName, Attributes attri){
     try{
      if(tagName.equalsIgnoreCase("employee")){
       System.out.println("\nMODE: " + attri.getValue("mode"));
      }
      if(tagName.equalsIgnoreCase("name")) fName = true;
      if(tagName.equalsIgnoreCase("empid")) fEmpId = true;
      if(tagName.equalsIgnoreCase("designation")) fDesig = true;
      if(tagName.equalsIgnoreCase("technology")) fTech = true;
     }catch (Exception e) {
      e.printStackTrace();
     }     
    }
    
    public void characters(char chars[], int id, int size) throws SAXException {
     if (fName) {
      System.out.println("NAME: " + new String(chars, id, size));
      fName = false;
     }else if (fEmpId) {
      System.out.println("EMP_ID: " + new String(chars, id, size));
      fEmpId = false;
     }else if (fDesig) {
      System.out.println("DESIGNATION: " + new String(chars, id, size));
      fDesig = false;
     }else if (fTech) {
      System.out.println("TECHNOLOGY: " + new String(chars, id, size));
      fTech = false;
     }    
    }
   };
   
  }catch (Exception e) {
   e.printStackTrace();
  }
  return handler;
 }
}


OUTPUT:

MODE: permanent
NAME: John
EMP_ID: 1234
DESIGNATION: SE
TECHNOLOGY: Java

MODE: contract
NAME: David
EMP_ID: 4545
DESIGNATION: Manager
TECHNOLOGY: .NET