java对xml 增删改_Java使用DOM4J对XML文件进行增删改查操作

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;

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值