一、新增对象
在XML文件中,以对象的方式存储数据。如下例子中,用户user中有三个属性username,password,nickname。我们需要把user对象存进去。
源代码如下所示:
package org.wangkeqing.test;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Element;
import org.wangkeqing.util.LogUtil;
import org.wangkeqing.util.XmlUtil;
public class TestXML {
public static void main(String[] args) {
//获取xml文件路径
String path = XmlUtil.class.getClass().getResource("/").getPath();
path = path.replace("bin", "src"); //Java项目而言
path = path.replaceAll("%20", " ");
path = path+"users.xml";
Document document = XmlUtil.getXmlDocument("users.xml",true);
Element rootElement = document.getRootElement();
//为根元素添加子元素user
Element element = rootElement.addElement("user");
element.addElement("username").addText("zhangsan");
element.addElement("password").addText("123456");
element.addElement("nickname").addText("张三");
//XmlUtil.write2XML(document,path,"UTF-8");
XmlUtil.write2XML(document, path);
}
}
程序运行后,xml文件如下:
二、修改对象内容
通过找到username的值,选择该user对象,进行修改password和nickname,源代码如下:
package org.wangkeqing.test;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Element;
import org.wangkeqing.util.LogUtil;
import org.wangkeqing.util.XmlUtil;
public class TestXML {
public static void main(String[] args) {
//获取xml文件路径
String path = XmlUtil.class.getClass().getResource("/").getPath();
path = path.replace("bin", "src"); //Java项目而言
path = path.replaceAll("%20", " ");
path = path+"users.xml";
Document document = XmlUtil.getXmlDocument("users.xml",true);
//修改对象,更改节点内容(根据username的值,选择更改的user对象)
String code="/users/user[username='lisi']";
Element e=(Element)document.selectSingleNode(code);
e.element("password").setText("A007");
e.element("nickname").setText("自动化测试技术");
XmlUtil.write2XML(document, path);
}
}
XML文件运行前后如下:
三、删除对象
通过document查询出相应的user对象,然后直接从根元素中remove掉该user就行。源代码如下。
package org.wangkeqing.test;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Element;
import org.wangkeqing.util.LogUtil;
import org.wangkeqing.util.XmlUtil;
public class TestXML {
public static void main(String[] args) {
//获取xml文件路径
String path = XmlUtil.class.getClass().getResource("/").getPath();
path = path.replace("bin", "src"); //Java项目而言
path = path.replaceAll("%20", " ");
path = path+"users.xml";
Document document = XmlUtil.getXmlDocument("users.xml",true);
//删除某个对象
String code="/users/user[username='wangwu']";
Element e=(Element)document.selectSingleNode(code);
//然后从根节点中把这个节点给删除
rootElement.remove(e);
XmlUtil.write2XML(document, path);
}
}
程序运行后,xml文件
四、查询对象
通过document对象查询出相应的user,例如:查询用户名为lisi的用户,最后输出到控制台。源代码如下:
package org.wangkeqing.test;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Element;
import org.wangkeqing.util.LogUtil;
import org.wangkeqing.util.XmlUtil;
public class TestXML {
public static void main(String[] args) {
//获取xml文件路径
String path = XmlUtil.class.getClass().getResource("/").getPath();
path = path.replace("bin", "src"); //Java项目而言
path = path.replaceAll("%20", " ");
path = path+"users.xml";
Document document = XmlUtil.getXmlDocument("users.xml",true);
String code = "/users/user[username='lisi']";
Element es = (Element)document.selectSingleNode(code);
if(es!=null){
LogUtil.info("==查询结果=="+es.element("password").getText()+"==="+es.element("nickname").getText());
}else{
LogUtil.info("==查询结果==您查询的对象不存在");
};
}
}
输出结果:2017-01-12 15:03:25.566 INFO ==查询结果==A007===自动化测试技术
同理,如果需要查询多个对象直接使用document的selectNodes(code)方法查询。