Thursday, April 18, 2013

Parse XML using java


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bd.com.pritom.xml.XmlSimpleParser;

import java.io.ByteArrayInputStream;
import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 *
 * @author User
 */
public class ParseXmlString {
    public static void main(String[] args) throws Exception {
        String xmlString = "<?xml version=\"1.0\"?>\n" +
            "<Users>\n" +
            " <User id=\"1001\">\n" +
            "  <firstname>Pritom</firstname>\n" +
            "  <lastname>Kumar Mondal</lastname>\n" +
            "  <nickname>pritom</nickname>\n" +
            " </User>\n" +
            " <User id=\"2001\">\n" +
            "  <firstname>Pallob</firstname>\n" +
            "  <lastname>Mondal</lastname>\n" +
            "  <nickname>mondal</nickname>\n" +
            " </User>\n" +
            "</Users>";
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        
        /* If you want to parse xml from a string variable */
        Document doc = dBuilder.parse( new ByteArrayInputStream(xmlString.getBytes()) );
        /* If you want to parse xml from file */
        /* File fXmlFile = new File("C:\\tmp\\Users.xml");
        Document doc = dBuilder.parse(fXmlFile); */
        
        doc.getDocumentElement().normalize(); 
        System.out.println("Root element :" + doc.getDocumentElement().getNodeName()); 
        NodeList nList = doc.getElementsByTagName("User");
        for (int temp = 0; temp < nList.getLength(); temp++) {
            Node nNode = nList.item(temp);
            System.out.println("----------------------------");
            System.out.println("Current Element :" + nNode.getNodeName());
            if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                Element eElement = (Element) nNode;
                System.out.println("User id : " + eElement.getAttribute("id"));
                System.out.println("First Name : " + eElement.getElementsByTagName("firstname").item(0).getTextContent());
                System.out.println("Last Name : " + eElement.getElementsByTagName("lastname").item(0).getTextContent());
                System.out.println("Nick Name : " + eElement.getElementsByTagName("nickname").item(0).getTextContent());
            }
        }
    }
}

And output would be like this:


Root element :Users
----------------------------
Current Element :User
User id : 1001
First Name : Pritom
Last Name : Kumar Mondal
Nick Name : pritom
----------------------------
Current Element :User
User id : 2001
First Name : Pallob
Last Name : Mondal
Nick Name : mondal

This is a simple xml parse technique. If you want recursively want to parse an unknown xml (xml tag unknow to you, any type of xml) and store to a java hashmap, please follow the link, full source code written for you.

No comments:

Post a Comment