xml parsing in java code description

NogTeetryNert

New Member
below is my java code for xml parsing. i need the description of each method what they doing java code :\[code\]package com.ohile.xmlparser;import java.io.IOException;import java.net.URL;import java.util.ArrayList;import java.util.Collections;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.Node;import org.w3c.dom.NodeList;import org.xml.sax.InputSource;import org.xml.sax.SAXException;import android.os.Bundle;import org.w3c.dom.Element;import android.annotation.SuppressLint;import android.app.Activity;import android.view.Menu;public class XMLParser extends Activity { Document dom; public XMLParser() { parseXmlFile(); ArrayList<Element> listGroups = getGroups(); // System.out.println(listGroups); for (Element group : listGroups) { System.out.println("Empid : " + getNodeValue(group, "Empid")); System.out.println("Phone : " + getNodeValue(group, "Phone")); System.out .println("Employee Name : " + getNodeValue(group, "Name")); System.out.println("DesignId : " + getNodeValue(group, "Desigid")); System.out.println("EmailId : " + getNodeValue(group, "Emailid")); System.out.println("ProjectId : " + getNodeValue(group, "Projectid")); System.out.println("Project Name: " + getNodeValue(group, "Projectname")); System.out.println("Display Level : " + getNodeValue(group, "Displaylevel")); // System.out.println("Phone : " + getNodeValue(group, "Phone")); } } private ArrayList getGroups() { try { Element documentElement = (Element) dom.getDocumentElement(); Element commonEle = getChildElement(documentElement, "Common"); Iterable<Element> groupElements = getChildren(commonEle, "Group"); ArrayList<Element> list = new ArrayList<Element>(); for (Element e : groupElements) { list.add(e); } return list; } catch (Exception e) { } return null; } public static boolean isVoid(Object o) { if (o == null) { return true; } else if (o instanceof String) { return ((String) o).trim().length() == 0; } else { return false; } } private static String getNodeValue(Element parentElement, String eleName) { Element childElement = getChildElement(parentElement, eleName); return ((Node) childElement).getFirstChild().getNodeValue(); } public static Iterable<Element> getChildren(Element parent, String tagName) { List<Element> ret = new ArrayList<Element>(); for (Node n = ((Node) parent).getFirstChild(); n != null; n = n .getNextSibling()) { if (n.getNodeType() == Node.ELEMENT_NODE) { Element child = (Element) n; if (tagName == null || tagName.equals(((org.w3c.dom.Element) child) .getTagName())) { ret.add(child); } } } return Collections.unmodifiableCollection(ret); } public static Element getChildElement(Element parentEle, String childName) { // System.out.println("inside child"); if (parentEle != null && !isVoid(childName)) { for (Node n = ((Node) parentEle).getFirstChild(); n != null; n = (Node) n.getNextSibling()) { if (n.getNodeType() == Node.ELEMENT_NODE && n.getNodeName().equals(childName)) { return (Element) n; } } } return null; } private void parseXmlFile() { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); // System.out.println("Inside xml"); try { String sURL = "http://jjjskaJSabre"; URL url = new URL(sURL); DocumentBuilder db = dbf.newDocumentBuilder(); dom = db.parse(new InputSource(url.openStream())); } catch (ParserConfigurationException pce) { pce.printStackTrace(); } catch (SAXException se) { se.printStackTrace(); } catch (IOException ioe) { ioe.printStackTrace(); } }}\[/code\]xml code :This XML file does not appear to have any style information associated with it. The document tree is shown below.\[code\]<root><Budget>100000.0</Budget><Procost><Costid>1</Costid><Costname>Fixed Cost</Costname><Amount>70666.66</Amount></Procost><Procostcount>1</Procostcount><Totalcost>70666.66</Totalcost><Common><Group><Empid>370</Empid><Reportid>-1</Reportid><Empcost>0</Empcost><Isanyreporting>Y</Isanyreporting><Reportcount>9</Reportcount><Reportname>NA</Reportname><Name>Shalini</Name><Desigid>0</Desigid><Gender>female</Gender><Yearexperience>11</Yearexperience><Monthexperience>0</Monthexperience><Phone>974113524</Phone><Emailid>[email protected]</Emailid><Projectid>1</Projectid><Projectname>Sabre</Projectname><Displaylevel>0</Displaylevel><Photo>None</Photo></Group><Group><Empid>381</Empid><Reportid>370</Reportid><Empcost>0</Empcost><Reportcount>0</Reportcount><Isanyreporting>No</Isanyreporting><Reportname>Shalini</Reportname><Name>Arun</Name><Desigid>1</Desigid><Gender>male</Gender><Yearexperience>8</Yearexperience><Monthexperience>0</Monthexperience><Phone>2147483647</Phone><Emailid>[email protected]</Emailid><Projectid>1</Projectid><Projectname>Sabre</Projectname><Displaylevel>0</Displaylevel><Photo>None</Photo></Group><Group><Empid>382</Empid><Reportid>370</Reportid><Empcost>0</Empcost><Reportcount>0</Reportcount><Isanyreporting>No</Isanyreporting><Reportname>Shalini</Reportname><Name>Manu</Name><Desigid>1</Desigid><Gender>male</Gender><Yearexperience>8</Yearexperience><Monthexperience>0</Monthexperience><Phone>123487509</Phone><Emailid>[email protected]</Emailid><Projectid>1</Projectid><Projectname>Sabre</Projectname><Displaylevel>0</Displaylevel><Photo>None</Photo></Group><Group><Empid>383</Empid><Reportid>370</Reportid><Empcost>0</Empcost><Reportcount>0</Reportcount><Isanyreporting>No</Isanyreporting><Reportname>Shalini</Reportname><Name>Ramakrishnan Nair</Name><Desigid>1</Desigid><Gender>male</Gender><Yearexperience>8</Yearexperience><Monthexperience>0</Monthexperience><Phone>467983245</Phone><Emailid>[email protected]</Emailid><Projectid>1</Projectid><Projectname>Sabre</Projectname><Displaylevel>0</Displaylevel><Photo>None</Photo></Group></Common><Project><Empid>371</Empid><Reportid>370</Reportid><Displaylevel>2</Displaylevel><Empcost>35333.3</Empcost><Name>Remya</Name><Desigid>5</Desigid><Gender>female</Gender><Yearexperience>2</Yearexperience><Monthexperience>0</Monthexperience><Phone>975443452</Phone><Emailid>[email protected]</Emailid><Projectid>1</Projectid><Projectname>Sabre</Projectname><Photo>None</Photo><Reportcount>0</Reportcount><Isanyreporting>No</Isanyreporting><Reportname>Shalini</Reportname></Project><Project><Empid>374</Empid><Reportid>370</Reportid><Displaylevel>2</Displaylevel><Empcost>35333.3</Empcost><Name>Nijo Susan Thomas</Name><Desigid>5</Desigid><Gender>female</Gender><Yearexperience>1</Yearexperience><Monthexperience>1</Monthexperience><Phone>2147483647</Phone><Emailid>[email protected]</Emailid><Projectid>1</Projectid><Projectname>Sabre</Projectname><Photo>None</Photo><Reportcount>0</Reportcount><Isanyreporting>No</Isanyreporting><Reportname>Shalini</Reportname></Project><Count>6</Count><Procount>2</Procount><Fieldnames><Fieldnamecount>16</Fieldnamecount><Group><Fieldname>Name</Fieldname></Group><Group><Fieldname>Empid</Fieldname></Group><Group><Fieldname>Desigid</Fieldname></Group><Group><Fieldname>Gender</Fieldname></Group><Group><Fieldname>Yearexperience</Fieldname></Group><Group><Fieldname>Monthexperience</Fieldname></Group><Group><Fieldname>Phone</Fieldname></Group><Group><Fieldname>Emailid</Fieldname></Group><Group><Fieldname>Projectid</Fieldname></Group><Group><Fieldname>Reportid</Fieldname></Group><Group><Fieldname>Isanyreporting</Fieldname></Group><Group><Fieldname>Reportcount</Fieldname></Group><Group><Fieldname>Reportname</Fieldname></Group><Group><Fieldname>Photo</Fieldname></Group><Group><Fieldname>Displaylevel</Fieldname></Group><Group><Fieldname>Empcost</Fieldname></Group></Fieldnames></root>\[/code\]output :\[code\]01-23 10:35:12.338: I/System.out(2487): Empid : 37001-23 10:35:12.338: I/System.out(2487): Phone : 97411352401-23 10:35:12.349: I/System.out(2487): Employee Name : Shalini01-23 10:35:12.359: I/System.out(2487): DesignId : 001-23 10:35:12.359: I/System.out(2487): EmailId : [email protected] 10:35:12.378: I/System.out(2487): ProjectId : 101-23 10:35:12.378: I/System.out(2487): Project Name: Sabre01-23 10:35:12.378: I/System.out(2487): Display Level : 001-23 10:35:12.378: I/System.out(2487): Empid : 38101-23 10:35:12.378: I/System.out(2487): Phone : 214748364701-23 10:35:12.378: I/System.out(2487): Employee Name : Arun01-23 10:35:12.378: I/System.out(2487): DesignId : 101-23 10:35:12.398: I/System.out(2487): EmailId : [email protected] 10:35:12.398: I/System.out(2487): ProjectId : 101-23 10:35:12.398: I/System.out(2487): Project Name: Sabre01-23 10:35:12.398: I/System.out(2487): Display Level : 001-23 10:35:12.398: I/System.out(2487): Empid : 38201-23 10:35:12.430: I/System.out(2487): Phone : 12348750901-23 10:35:12.438: I/System.out(2487): Employee Name : Manu01-23 10:35:12.438: I/System.out(2487): DesignId : 101-23 10:35:12.468: I/System.out(2487): EmailId : [email protected] 10:35:12.478: I/System.out(2487): ProjectId : 101-23 10:35:12.501: I/System.out(2487): Project Name: Sabre01-23 10:35:12.501: I/System.out(2487): Display Level : 001-23 10:35:12.501: I/System.out(2487): Empid : 38301-23 10:35:12.501: I/System.out(2487): Phone : 46798324501-23 10:35:12.501: I/System.out(2487): Employee Name : Ramakrishnan Nair01-23 10:35:12.508: I/System.out(2487): DesignId : 101-23 10:35:12.508: I/System.out(2487): EmailId : [email protected] 10:35:12.508: I/System.out(2487): ProjectId : 101-23 10:35:12.518: I/System.out(2487): Project Name: Sabre01-23 10:35:12.528: I/System.out(2487): Display Level : 0\[/code\]
 
Back
Top