使用dom4j操作xml实现增删改查

时间:2022-10-21 00:05:51

         dom4j提供了一套操作xml文档的API,下面我们就实现一下最基本的增删改查操作。

       先看下最开始的xml文档

      

<?xml version="1.0" encoding="utf-8"?>

<students>
<student>
<name minzu="yyy">诗展</name>
<id>1</id>
<age>22</age>
</student>
<student>
<name minzu="hanzu">嘿嘿</name>
<id>2</id>
<age>23</age>
</student>
</students>

  1:查找,dom4j不能直接查找某个标签,不像html dom操作一样有getElementById之类的函数,他查找某个标签,只能从根结点开始遍历,知道查找到我们需要的标签,所以这点dom4j用起来很繁琐。下面是代码:

 

//读取某个标签下的子标签
static void readXml() throws DocumentException
{
SAXReader reader = new SAXReader();
Document document = reader.read(new File("src/student.xml"));
//取得根结点
Element root = document.getRootElement();
//取得根结点下面的所有student结点
Element e1 = (Element) root.elements("student").get(1);
//取得student结点下面的name结点
String name = e1.element("name").getText();
//取得name结点的属性结点
String minzu = e1.element("name").attributeValue("minzu");
System.out.println(name);

System.out.println(minzu);
}

我们看到的输出结果是:

                                     嘿嘿
                                     hanzu


   2:实现添加标签到末尾,我们只需要取得父结点,然后调用父结点的addElement(String name)就可以了。

 

//写入末尾
static void writeXml() throws DocumentException, IOException
{
SAXReader reader = new SAXReader();
Document document = reader.read(new File("src/student.xml"));

//要写入的内容,在students标签的第二个student标签内写入address属性,并且赋值为泉州
Element student = (Element)
document.getRootElement().elements("student").get(1);
//插入标签,且设置值
student.addElement("address").setText("泉州");

OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("utf-8");

//FileOutputStream的编码也是按照format设置的字符集进行编码
XMLWriter writer = new XMLWriter(
new FileOutputStream( "src/student.xml" ), format );
writer.write( document);
writer.close();
}
  更新后的xml文档,我们可以看到多了一个address标签

 

<?xml version="1.0" encoding="utf-8"?>

<students>
<student>
<name minzu="yyy">诗展</name>
<id>1</id>
<age>22</age>
</student>
<student>
<name minzu="hanzu">嘿嘿</name>
<id>2</id>
<age>23</age>
<address>泉州</address>
</student>
</students>

   3:实现插入到特定位置,dom4j没有insertbefore类似的方法,要实现插入到特定位置,需要取得结点下的所有子结点的list集合,然后修改list集合,实现特定位置插入。

 

//往指定位置插入新的结点,dom4j中没有insertbefore方法,要在指定位置插入,必须取得父节点
//的elements集合,然后修改elements集合,实现指定位置插入
static void insertToXml() throws DocumentException, IOException
{
SAXReader reader = new SAXReader();
Document document = reader.read(new File("src/student.xml"));

//得到第二个student标签
Element student = (Element)
document.getRootElement().elements("student").get(1);

List list = student.elements(); //得到所有子结点[name,id,age]

//创建新的结点
Element addressnode = DocumentHelper.createElement("address");
addressnode.setText("泉州");
//插入新的结点,list变成了[name,address,id,age],原来1后面的元素往后移动了一个位置
list.add(1,addressnode);

//重新写入
//如果我们的xml编码格式为gb2312的话,那么字符集应该设置成format.setEncoding("gb2312");
//也就是xml的字符集和匹配器设置的字符集得一致
OutputFormat format = OutputFormat.createPrettyPrint();
//这里指定的格式要和xml文档的编码格式一致,否则会出现乱码
format.setEncoding("utf-8");

//FileOutputStream的编码也是按照format设置的字符集进行编码
XMLWriter writer = new XMLWriter(
new FileOutputStream( "src/student.xml" ), format );

//写入
writer.write( document);
//关闭
writer.close();
}

   4:实现删除

  

//删除xml中的结点
static void delete() throws DocumentException, IOException
{
SAXReader reader = new SAXReader();
Document document = reader.read(new File("src/student.xml"));

List studentlist = document.getRootElement().elements("student");
Element student2 = (Element) studentlist.get(1);
//取得所有属性
List attributes = student2.elements();
Element e = (Element) attributes.get(1);

//实现删除,取得父亲,删除自己
e.getParent().remove(e);


OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("utf-8");


XMLWriter writer = new XMLWriter(
new FileOutputStream( "src/student.xml" ), format );
writer.write( document);
writer.close();
}

   5:实现更新

  

static void update() throws DocumentException, IOException
{
SAXReader reader = new SAXReader();
Document document = reader.read(new File("src/student.xml"));

List studentlist = document.getRootElement().elements("student");
Element student2 = (Element) studentlist.get(1);
//取得所有属性
List attributes = student2.elements();
Element e = (Element) attributes.get(1);
//更新其中的文本内容
e.setText("222");

OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("utf-8");


XMLWriter writer = new XMLWriter(
new FileOutputStream( "src/student.xml" ), format );
writer.write( document);
writer.close();


}