Monday 29 October 2007

Dom & ArrayList example

package com.samsung.dpd.everest.presentation.sespsdk.sample.loginapp.authn.localdb;

import java.io.File;
import java.util.ArrayList;

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;

import com.samsung.dpd.everest.presentation.sespsdk.sample.loginapp.ui.LocalDbLocalUiServlet;

public class XMLparsing {

//private static final String _USER_DB_ = LocalDbLocalUiServlet._AUTHN_ +"/users.xml";
private static final String _USER_DB_ = "users.xml";

public XMLparsing() {
// TODO Auto-generated constructor stub
}

public static void main (String args []){
ArrayList v1 = new ArrayList();

UserAll ua = new UserAll();
XMLparsing s = new XMLparsing();
v1 = s.getInfoAll();
System.out.println("======================================");
for (int i =0 ; i < v1.size(); i++){
ua = (UserAll)v1.get(i);
System.out.println("11 : "+ ua.getUname());
}
}

public ArrayList getInfoAll() {

ArrayList v = new ArrayList();

try {
File file= new File(XMLparsing._USER_DB_);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(file);
doc.getDocumentElement().normalize();
System.out.println("Root element : "+doc.getDocumentElement().getNodeName());
NodeList nodeList = doc.getElementsByTagName("user");
System.out.println("Information of all users");

for (int s = 0; s < nodeList.getLength(); s++){
UserAll ua = new UserAll();
Node fstNode = nodeList.item(s);

if (fstNode.getNodeType() == Node.ELEMENT_NODE){
Element fstElmnt = (Element)fstNode;
NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("username");
Element fstNmElmnt = (Element)fstNmElmntLst.item(0);
NodeList fstNm = fstNmElmnt.getChildNodes();
System.out.println("user name : "+((Node)fstNm.item(0)).getNodeValue());
ua.setUname(((Node)fstNm.item(0)).getNodeValue());


NodeList fstNmElmntLst2 = fstElmnt.getElementsByTagName("userpassword");
Element fstNmElmnt2 = (Element)fstNmElmntLst2.item(0);
NodeList fstNm2 = fstNmElmnt2.getChildNodes();
System.out.println("user userpassword : "+((Node)fstNm2.item(0)).getNodeValue());
ua.setUpassword(((Node)fstNm2.item(0)).getNodeValue());

NodeList fstNmElmntLst3 = fstElmnt.getElementsByTagName("email");
Element fstNmElmnt3 = (Element)fstNmElmntLst3.item(0);
NodeList fstNm3 = fstNmElmnt3.getChildNodes();
System.out.println("user email : "+((Node)fstNm3.item(0)).getNodeValue());
ua.setUemail(((Node)fstNm3.item(0)).getNodeValue());

NodeList fstNmElmntLst4 = fstElmnt.getElementsByTagName("role");
Element fstNmElmnt4 = (Element)fstNmElmntLst4.item(0);
NodeList fstNm4 = fstNmElmnt4.getChildNodes();
System.out.println("user role : "+((Node)fstNm4.item(0)).getNodeValue());
ua.setUrole(((Node)fstNm4.item(0)).getNodeValue());
}
v.add(ua);
}


} catch (Exception e){
e.printStackTrace();
}

return v;
}

}

No comments: