java操作XML两种方式jdom和dom4j
作者:互联网
java操作XML两种方式jdom和dom4j
java提供的工具包可以操作XML,有dom操作方式和sax方式,这两种方式博主也写了文章,可以看我的博客~,基于DOM解析方式,是把整个XML文档加载到内存,转化成DOM树,然后访问DOM树的任何数据,具体使用DocumentBuilder来获取元素节点,然后获取值,这里介绍两种工具,jdom和dom4j,以便于快速开发XML应用程序
- jdom
jdom是一种使用XML的独特的java工具包
需要先下载jdom的jar包
这里给出使用jdom的两种案例
jdom生成xml
例如生成student.xml
<?xml version="1.0" encoding="UTF-8"?>
<student id="001">
<name>张三</name>
<sex>男</sex>
<age>22</age>
</student>
import java.io.FileOutputStream;
import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.XMLOutputter;
public class Jdom01 {
public static void main(String[] args) throws Exception {
//jdom主要是通过建立节点然后赋值,添加子节点父节点实现
//但是用jdom生成的xml里内容格式混乱,所以一般使用dom4j
Element student=new Element("student");
Attribute id=new Attribute("id", "001");
student.setAttribute(id);
Element name=new Element("name");
name.setText("张三");
student.addContent(name);
Element sex=new Element("sex");
sex.setText("男");
student.addContent(sex);
Element age=new Element("age");
age.setText("22");
student.addContent(age);
Document document=new Document(student);
XMLOutputter out=new XMLOutputter();
out.setFormat(out.getFormat().setEncoding("UTF-8"));
out.output(document, new FileOutputStream("src/student.xml"));
}
}
jdom遍历xml
students.xml
<?xml version="1.0" encoding="UTF-8"?>
<students>
<student id="001">
<name>张三</name>
<sex>男</sex>
<age>22</age>
</student>
<student id="002">
<name>李四</name>
<sex>女</sex>
<age>21</age>
</student>
</students>
import java.util.List;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;
public class Jdom02 {
public static void main(String[] args) throws Exception {
SAXBuilder builder=new SAXBuilder(); //jdom和dom4j都是用SAX来遍历xml的
Document document=builder.build("src/students.xml");
Element students=document.getRootElement(); //获取根节点
List<Element> studentList=students.getChildren();
for(int i=0;i<studentList.size();i++) {
Element student=studentList.get(i);
String id=student.getAttributeValue("id");
String name=student.getChildText("name");
String sex=student.getChildText("sex");
String age=student.getChildText("age");
System.out.println(id+" "+name+" "+sex+" "+age);
}
//相比于原始的方法,这种遍历更方便
}
}
- dom4j
dom4j是一个java的XML API,类似于jdom,用来读写XML文件的,许多框架就是使用dom4j来解析xml文件的,比如Hibernate框架
需要先下载dom4j的jar包
同样,也给出两种案例
dom4j生成xml
例如生成student.xml
<?xml version="1.0" encoding="UTF-8"?>
<student id="001">
<name>张三</name>
<sex>男</sex>
<age>22</age>
</student>
import java.io.FileOutputStream;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
public class DOM4J01 {
public static void main(String[] args) throws Exception {
//这样生成的xml文件是格式化过的,比jdom要好
Document document=DocumentHelper.createDocument();
Element student=document.addElement("student");
student.addAttribute("id", "001");
Element name=student.addElement("name");
name.setText("张三");
Element sex=student.addElement("sex");
sex.setText("男");
Element age=student.addElement("age");
age.setText("22");
OutputFormat format=OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8");
XMLWriter writer=new XMLWriter(new FileOutputStream("src/student.xml"),format);
writer.write(document);
writer.close();
}
}
dom4j遍历xml
students.xml
<?xml version="1.0" encoding="UTF-8"?>
<students>
<student id="001">
<name>张三</name>
<sex>男</sex>
<age>22</age>
</student>
<student id="002">
<name>李四</name>
<sex>女</sex>
<age>21</age>
</student>
</students>
import java.util.Iterator;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class DOM4J02 {
public static void main(String[] args) throws Exception {
SAXReader reader=new SAXReader();
Document document=reader.read("src/students.xml");
Element students=document.getRootElement();
Iterator<?> iterator=students.elementIterator();
while(iterator.hasNext()) {
Element student=(Element)iterator.next();
System.out.print("学号:"+student.attributeValue("id")+" ");
System.out.print("姓名:"+student.elementText("name")+" ");
System.out.print("性别:"+student.elementText("sex")+" ");
System.out.println("年龄:"+student.elementText("age"));
System.out.println("=================");
}
}
}
Dom4J性能要比jdom好,所以建议使用Dom4J
标签:XML,xml,java,dom4j,jdom,Element,student,import 来源: https://blog.csdn.net/weixin_44347814/article/details/112465940