packagecom.founder.mrp.util;importjava.io.File;importjava.util.ArrayList;importjava.util.List;importorg.dom4j.Document;importorg.dom4j.DocumentHelper;importorg.dom4j.Element;importorg.dom4j.Node;importcom.founder.mrp.domain.ClientLoginUser;importcom.founder.mrp.util.md5.MD5;public classXMLOperateUser {/** 查询全部xml
**/
public static List ListClinetLoginUser(String appDir) throwsException{
File dir= new File(appDir+"\\persons.xml");if (!dir.exists()) {
dir.createNewFile();
Document dom=DocumentHelper.createDocument();
Element root= dom.addElement("persons");
String dirpath= dir+"";
UtilsForXML.writeToXML(dom, dirpath);
}
String dirPath= dir+"";
Document dom=UtilsForXML.getDocument(dirPath);
Element root=dom.getRootElement();
List persons = new ArrayList();
List list=root.elements();for (int i = 0; i < list.size(); i++) {
Element person=(Element) list.get(i);
ClientLoginUser c= newClientLoginUser();
String id= person.attributeValue("id");
c.setId(Integer.parseInt(id));
List ll=person.elements();for (int j = 0; j < ll.size(); j++) {
Element element=(Element) ll.get(j);if ("publishername".equals(element.getName())) {
String publisherName=element.getText();
c.setPublisherName(publisherName);
}if ("serverurl".equals(element.getName())) {
String serverurl=element.getText();
c.setServerUrl(serverurl);
}if ("username".equals(element.getName())) {
String username=element.getText();
c.setUserName(username);
}if ("password".equals(element.getName())) {
String password=element.getText();
c.setPassword(password);
}
}
persons.add(c);
}returnpersons;
}/** 根据person的属性id查询xml
**/
public static ClientLoginUser QueryClinetLoginUserById(String appDir,int id) throwsException{
File dir= new File(appDir+"\\persons.xml");
String dirPath= dir+"";
Document dom=UtilsForXML.getDocument(dirPath);
Element root=dom.getRootElement();
ClientLoginUser person= newClientLoginUser();
Element beQuery= (Element)root.selectSingleNode("//person[@id="+id+"]");if(beQuery!=null){
person.setPublisherName(beQuery.elementText("publishername"));
person.setServerUrl(beQuery.elementText("serverurl"));
person.setUserName(beQuery.elementText("username"));
person.setPassword(beQuery.elementText("password"));
person.setId(id);
}returnperson;
}/** 增加xml数据
**/
public static int AddClinetLoginUser(String appDir,String publisherName,String serverUrl,String userName,String passWord) throwsException{
File dir= new File(appDir+"\\persons.xml");if (!dir.exists()) {
dir.createNewFile();
}int id = 1;
String dirPath= dir+"";
Document dom=UtilsForXML.getDocument(dirPath);
Element root=dom.getRootElement();
List list = root.elements("person");if(!list.isEmpty()||list.size()!=0){int count =list.size();
Element lastperson= list.get(count-1);
String value= lastperson.attributeValue("id");
id= Integer.parseInt(value)+1;
}//int id = (int) ((Math.random()*9+1)*1000);
Element newPerson = root.addElement("person");
newPerson.addAttribute("id", id+"");
Element publishername= newPerson.addElement("publishername");
publishername.setText(publisherName);
Element serverurl= newPerson.addElement("serverurl");
serverurl.setText(serverUrl);
Element username= newPerson.addElement("username");
username.setText(userName);
Element password= newPerson.addElement("password");
password.setText(passWord);
UtilsForXML.writeToXML(dom, dirPath);returnid;
}/** 根据person属性id修改xml数据
**/
public static int UpdateClinetLoginUser(int id,String appDir,String publisherName,String serverUrl,String userName,String passWord) throwsException{
File dir= new File(appDir+"\\persons.xml");
String dirPath= dir+"";
Document dom=UtilsForXML.getDocument(dirPath);
Element root=dom.getRootElement();
Element beQuery= (Element)root.selectSingleNode("//person[@id="+id+"]");
beQuery.element("publishername").setText(publisherName);
beQuery.element("serverurl").setText(serverUrl);
beQuery.element("username").setText(userName);
beQuery.element("password").setText(passWord);
UtilsForXML.writeToXML(dom, dirPath);returnid;
}/** 根据person属性id删除xml数据
**/
public static int DeleteClinetLoginUser(int id,String appDir) throwsException{
File dir= new File(appDir+"\\persons.xml");
String dirPath= dir+"";
Document dom=UtilsForXML.getDocument(dirPath);
Element root=dom.getRootElement();
Element beQuery= (Element)root.selectSingleNode("//person[@id="+id+"]");
beQuery.getParent().remove(beQuery);
UtilsForXML.writeToXML(dom, dirPath);returnid;
}
}