/*
* 使用dom4j实现修改节点的操作
* 修改第一个head下面的age元素的值<age>30</age>
* 1、得到document
* 2、得到根节点 然后得到第一个head元素
* 3、得到第一个head下面的age
* element("")方法
* 4、修改值30
* 使用setText("文本内容")方法
* 5、回写xml
* 使用dom4j实现删除节点的操作
* 删除第一个head下面的titile元素
*
* 1、得到document
* 2、得到根节点
* 3、得到第一个head标签
* 4、得到第一个head下面的title元素
* 5、删除(使用head删除title)
* 得到school的父节点
* 第一种直接得到head
* 使用方法 getParent方法得到
* 删除操作
* 在head上面执行remove方法删除节点
* 6、回写xml
* 使用dom4j获取属性的操作
* 获取第一个head里面的属性id的值
* 步骤
* 1、得到document
* 2、得到根节点
* 3、得到第一个head元素
* 4、得到head里面的属性值
* attributeValue("id");
* 在head上面执行这个方法 里面的参数是属性名称
*
* 使用dom4j支持xpath的操作
* 可以直接获取到某个元素
* 第一种形式
* /aa/bb/cc 表示一层一层的 aa下面 bb下面的cc
* 第二种形式
* //bbb 表示和这个名称相同表示只要名称是bbb都得到
* 第三种形式
* /* 所有元素
* 第四种形式
* bbb[1] 表示第一个bbb元素
* bbb[last()] 表示最后一个bbb元素
* 第五种形式
* //bb[@id] 表示只要bb元素上面有id属性 都得到
* 第六种形式
* //bbb[@id='b1'] 表示元素名称是bbb 在bbb上面有id属性 并且id属性是b1
* 使用dom4j支持xpath具体操作
* 默认的情况下 dom4j不支持xpath
* 如果想要在dom4j里面是有xpath
* 第一步需要 引入支持xpath的jar包 使用 jaxen-1.1.6.jar
* 需要把jar包导入文件
* 在dom4j里面提供了两个方法 用来支持xpath
* selectNodes("xpath表达式")
* 获取多个节点
* selectSingleNode("xpath表达式")
* 获取一个节点
* 使用xpath实现 查询xml所有name元素的值
* 所有name元素的xpath表示
* 使用 selectNodes("//name")
* 1、得到document
2、直接使用selectNodes("//name") 方法得到所有的name
//得到document
Document document = domUtils.document();
//使用selecNodes()
List<Node> list = document.selectNodes("//title");
//遍历list集合
for (Node node : list) {
//node是每一个name元素
//得到name元素里面的值
String s = node.getText();
System.out.println(s);
*使用xpath实现 获取第一个head下面的title的值
* //head[@id="1"]/title
* 使用到selectSingleNode("//head[@id="1"]/title")
* 步骤和代码
*
1、得到document
2、直接使用selectSingleNode 方法实现
xpath : //head[@id="1"]/title
//得到document
Document document = domUtils.document();
//直接使用selectSingleNode 方法实现 双引号里面用单引号来表示属性值
Node name = document.selectSingleNode("//head[@id='1']/title");
//得到name里面的值
String s = name.getText();
System.out.println(s);
*/
public static void delHead(String id) throws Exception {
SAXReader saxr = new SAXReader();
Document document = saxr.read("src/ee/11.xml");
List<Node> list = document.selectNodes("//id");
for (Node node : list) {
String id1 = node.getText();
if(id1.equals(id)) {
Element head = node.getParent();
Element rest = head.getParent();
rest.remove(head);
}
}
OutputFormat of = OutputFormat.createPrettyPrint();
XMLWriter xmlw = new XMLWriter(new FileOutputStream("src/ee/11.xml"),of);
xmlw.write(document);
xmlw.close();
}
public static void addAbc() throws Exception{
SAXReader saxr = new SAXReader();
Document document = saxr.read("src/ee/11.xml");
Element root = document.getRootElement();
Element abc = root.addElement("abc");
Element a = abc.addElement("a");
Element b = abc.addElement("b");
Element c = abc.addElement("c");
a.setText("a1");
b.setText("b1");
c.setText("c1");
OutputFormat of = OutputFormat.createPrettyPrint();
XMLWriter xmlw = new XMLWriter(new FileOutputStream("src/ee/11.xml"),of);
xmlw.write(document);
xmlw.close();
}
private static void test2() throws Exception {
Document document = domUtils.document();
Node name = document.selectSingleNode("//head[@id='1']/title");
String s = name.getText();
System.out.println(s);
}
public static void test1() throws Exception{
Document document = domUtils.document();
List<Node> list = document.selectNodes("//title");
for (Node node : list) {
String s = node.getText();
System.out.println(s);
}
}