用DOM4J解析XML文件案例,由于DOM4J不像JAXP属于JAVASE里,所以如果要使用DOM4J,则必须额外引入jar包,如图:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAqAAAAAgCAIAAABCcV1jAAAIBUlEQVR4nO2ca2/TVhjH87XyDfZF8rJfYFJfdaqnSVgVdBsvVgIZo2JKCiXKatwLAVomJdCoUldlk6YGaCbWcSm5Oo4d7YVj+/hc7DgX16T/n/wiPT4+5znGOT+fxw4JAwAAAABzR+KyAwAAAADA9IHgAQAAgDkEggcAAADmEAgeAAAAmEMgeAAAAGAOgeABAACAOQSCBwAAAOYQP8H39P5W+e213PFi5hAbNmzYsGGL7XYtd7xVftvT+5HpM/74CX7joFYovfnQ0EwAAAAgxnxoaIXSm42DWmT6jD9+gl9aP+pqnruhopi9vb3t7e0ZRwsAAADw6Wr9pfWjy44iRvgJfjFzaJomWVIsFo1+3zD6pmmYpjEYmIOBaRj9wcAslUqqqqrb6owDBgAAADiYprmYObzsKGJEOME/efKk39dZxw8G5sHBQafTURRld3d3xjEDAAAANBA8RTjB7+7u6nqP63hVVRVFURRFVYMW8flUIilXJ4+doionZ9JunHoEAERKXZEW0hW2vJLmFkdAXZEWJKV+qTHEFQieIpzgd3Z29J729LBaOjktn9TKJ7XSH6flk1rp5PRZ5c9/6vXa6Wmkgq/KyYTdmL9u86lEIpHK++1NJARVRMdC8FeWuiItDCGm2Ep6WGbNv55iciZ26rF1uR0Jq/DD4PRIw63gf9QkoxgTYnjMEKNiSoKvpMc9Rd6TICn1uAveuS64F8vwLFD/tNwryDM864B0xfC98CB4inCCV1W1p3WL5WPrT13XP71I67puGEaxfGwt5RVFCehzaoKvyslkMtix1l1AKpUUCN4vnqBjwVWkknamFXf+d+ddo5JeIAoXpHRaome70Wb7uiJJkiSqLAyD7dHbJlvB/6gJRzE2IrlG2UUsBC/oKYaCryvSMCT3S8D/angP4g2EGJ7nMPJcUu1B8BThBK8oitbtbD1/pWlat9v97/F35/u3P1UeaZq29fx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TlJFbw8w43J03MVfSEyyQpyYxv0NQsnr19UuMRpOjDjWIs+KOwTojozWw7LLKwogyzIuRvtBizUo92SSGTNwac9DjbO2cg7PMEfhiBJ4Gfog+MIRLcl+jp8839avjfmVBv0fNyWljBBxBO8Gtra81Go9VotJvNdqvVabe67Xa309G63Z6m6T1N13tra2ucttx36ZKynPJaXfCS3dQET6/4U3lK4cyv4L36Fx7rXawPh+f+pA6Cn1PojKhnwmafP3LW4fxcu7A3YdaXE4agRxduBV6hO0NPYRRjQfU7TL0LNM2Ml/nncArIX/wNC+0HI0yFuiItpNPMO3akfbi9G249UVh0L+MLPiCGKOEHwvlqBL+7waT2F5yMgOgCh+Apwgl+ZWWl+flzs/HZdnzT6/iu3tNWVlZmHLOA6H0KgwMAQGyA4CnCCX55eblxceE4vsVz/PLy8oxj5oP/qRYAAK4yEDxFOMF/PRozjplhmGKPcDEdfY8AAAB8geApwgkeAAAAiCcQPIWf4KX7R412L7JQAAAAgLFptHvS/aPLjiJG+An+0e+vlZdnXa1vAgAAADGmq/WVl2cPX7yOTJ/xx0/wHU3P7deW1o8WM4fYsGHDhg1bbLel9aPcfq2j6ZHpM/74CR4AAAAAXyjhBF8Us7e3t729PaMoAQAAABCK/wGdTt1yJDwN4AAAAABJRU5ErkJggg==" alt="" />
1.xml文件
<?xml version="1.0" encoding="UTF-8"?> <class>
<student>
<name>张三</name>
<sid>111111</sid>
</student>
<student>
<name>李四</name>
<sid>222222</sid>
</student>
</class>
Java代码:
import java.io.FileOutputStream;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter; public class Dom4jCase {
static Element root;
public static void main(String[] args) throws Exception {
add();//在第一个学生里面增加性别标签<sex>男</sex>
//add2();//在第一个学生的<name>张三</name>标签后增加<home>沭阳</home>
//delete();//删除上面增加的性别标签<sex>男</sex>
//modify();//将上面增加的标签<home>沭阳</home>改为<home>江苏</home>
//select();//查询所有学生姓名
}
//在第一个学生里面增加性别标签<sex>男</sex>
private static void add() throws Exception {
Document document = getRoot();
Element s1=root.element("student");
Element sex1=s1.addElement("sex");
sex1.setText("男");
writeBack(document);
}
//在第一个学生的<name>张三</name>标签后增加<home>沭阳</home>
private static void add2() throws Exception {
Document document = getRoot();
Element s1=root.element("student");
List<Element>list=s1.elements();
Element home=DocumentHelper.createElement("home");
home.setText("沭阳");
list.add(1,home );
writeBack(document);
}
//删除上面增加的性别标签<sex>男</sex>
private static void delete() throws Exception{
Document document = getRoot();
Element s1=root.element("student");
s1.remove(s1.element("sex"));
writeBack(document);
}
//将上面增加的标签<home>沭阳</home>改为<home>江苏</home>
private static void modify() throws Exception{
Document document = getRoot();
Element s1=root.element("student");
s1.element("home").setText("江苏");
writeBack(document);
}
//查询所有学生姓名
private static void select() throws Exception{
Document document = getRoot();
List<Element> list=root.elements("student");
for(int i=0;i<list.size();i++)
{
System.out.println(list.get(i).elementText("name"));
}
}
//XML回写
private static void writeBack(Document document)throws Exception {
OutputFormat format=OutputFormat.createPrettyPrint();
XMLWriter writer=new XMLWriter(new FileOutputStream("src/1.xml"), format);
writer.write(document);
writer.close();
}
//初始化
private static Document getRoot() throws DocumentException {
SAXReader saxReader=new SAXReader();
Document document=saxReader.read("src/1.xml");
root=document.getRootElement();
return document;
}
}