//dom4j
exam.xml | output.xml |
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<exam>
<student idcard="111" examid="222">
<name>张三</name>
<location>沈阳</location>
<grade>89</grade>
</student>
<student idcard="333" examid="444">
<name>李四</name>
<location>大连</location>
<grade>97</grade>
</student>
</exam>
|
<?xml version="1.0" encoding="utf-8"?>
<exam>
<student idcard="111" examid="222">
<name>张三</name>
<location>沈阳</location>
<grade>89</grade>
<sex>男</sex>
</student>
<student idcard="333" examid="444">
<name>李四</name>
<location>大连</location>
</student>
<student idcard="5555" examid="999"/>
</exam>
|
Dom4j.java | |
/* 针对exam.xml文件使用dom4j完成入下操作:
1,得到某个具体的节点内容
2、修改某个元素节点的主体内容
3、向指定元素节点中增加子元素节点
4、向指定元素节点上增加同级元素节点
5、删除指定元素节点
6、遍历所有元素节点
7、获取XML文件某元素属性
*/
|
//开头导入包省略 public class Dom4j {
public static void main(String[] args) throws Exception{
SAXReader reader=new SAXReader();
Document document=reader.read("exam.xml"); //获取document对象
//得到第一个人的姓名
Element rootElement = document.getRootElement();
Element firstStudent=rootElement.element("student");
Element firstName=firstStudent.element("name");
String firstNameText = firstName.getText();
System.out .println(firstNameText);
|
//修改第二个人的grade为100
List allStudentList = rootElement.elements();
Element secondStudent=(Element)allStudentList.get(1);
Element secGrade=secondStudent.element("grade");
secGrade.setText("100");
//第一个学生增加性别 - 男
Element firstStuSex= firstStudent.addElement("sex");
firstStuSex.setText("男");
//根节点增加同级元素 一个新的student
Element createnewElement = DocumentHelper.createElement("student");
createnewElement.setAttributeValue("idcard", "5555");
createnewElement.setAttributeValue("examid", "999");
rootElement.add(createnewElement);
//rootElement.addElement("student"); //第二种方式
//删除第二个学生的grade
secGrade.detach(); //remove()也行
|
//遍历所有元素
for(int i=0;i<allStudentList.size();++i){
Element tmpStu=(Element)allStudentList.get(i);
for(int j=0;j<tmpStu.elements().size();j++){
Element tmpChild=(Element)tmpStu.elements().get(j);
String str=tmpChild.getName();
String str1=tmpChild.getText();
System.out .println(str+" "+str1);
}
}
//获取XML属性
String xmlEncoding=document.getXMLEncoding();
String UniquePath=document.getUniquePath();
System.out .println(xmlEncoding+" "+UniquePath); //在当前目录
//标准的保存语句。
//dom4j写回文档会自动写换行方便观看
OutputFormat format = OutputFormat.createPrettyPrint(); // 指定XML编码
format.setEncoding("utf-8");
XMLWriter writer = new XMLWriter(new FileOutputStream ("output.xml"),format);
writer.write(document);
writer.close();
}
}
|
//XPath
exam.xml | output2.xml |
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<exam>
<student idcard="111" examid="222">
<name>张三</name>
<location>沈阳</location>
<grade>89</grade>
</student>
<student idcard="333" examid="444">
<name>李四</name>
<location>大连</location>
<grade>97</grade>
</student>
</exam>
|
<?xml version="1.0" encoding="utf-8"?>
<exam>
<student idcard="111" examid="222">
<name>张三</name>
<location>沈阳</location>
<grade>100</grade>
</student>
<student idcard="333" examid="444">
<name>李四</name>
<location>大连</location>
</student>
</exam>
|
public class XPath {
public static void main(String[] args) throws Exception{
SAXReader reader = new SAXReader();
Document document = reader.read("exam.xml");
//得到第一个人的姓名
Node firstStuNameNode=document.selectSingleNode("/exam/student/name");
String firstStuName=firstStuNameNode.getText();
System.out.println(firstStuName);
//修改第二个人的grade为100
Node firstStuGradeNode = document.selectSingleNode("/exam/student/grade");
firstStuGradeNode.setText("100");
//删除第二个学生的grade
Node secStuNode=document.selectSingleNode("/exam/student[2]/grade");
secStuNode.detach();
//遍历所有元素
List allNodes=document.selectNodes("/exam/*");
for(int i=0;i<allNodes.size();++i){
Element subNodes=(Element)allNodes.get(i);
for(int j=0;j<subNodes.elements().size();++j){
String subName=((Element)subNodes.elements().get(j)).getName();
String subText=((Element)subNodes.elements().get(j)).getText();
System.out .println(subName+" "+subText);
}
}
|
//标准的保存语句。
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("utf-8");
XMLWriter writer = new XMLWriter(new FileOutputStream ("output2.xml"),format);
writer.write(document);
writer.close();
}
}
//目前我还只会用XPath来定位,就比DOM4j简单的多,增加还不会
|