1. JDOM 引入
- JDOM 是一种使用 XML(标准通用标记语言下的一个子集) 的独特 Java 工具包,用于快速开发 XML 应用 程序。
- 集合DOM 和 SAX 的优点
- 需要引入jar包
- JDOM 官方网站:http://www.jdom.org/
- 小缺陷:生成文档不会格式化,需要手动格式化 ( ctrl+shift+F)
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
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) {
Element student=new Element("student");
Attribute id=new Attribute("id", "001"); // 属性节点
Attribute aa=new Attribute("aa", "xx");
student.setAttribute(id);
student.setAttribute(aa);
Element name=new Element("name"); //元素节点名
name.setText("张三"); // 设置文本节点
student.addContent(name);
Document document=new Document(student);
XMLOutputter out=new XMLOutputter();
out.setFormat(out.getFormat().setEncoding("UTF-8"));
try {
out.output(document, new FileOutputStream("src/student2.xml"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
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(); // SAXBuilder
Document document=builder.build("src/students.xml");
Element students=document.getRootElement();
List studentList=students.getChildren("student");
for(int i=0;i<studentList.size();i++){
Element student=(Element)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);
}
}
}
2. DOM4J 引入
- dom4j 是一个 Java 的 XML API,类似于 jdom,用来读写 XML 文件的。
- dom4j 是一个非常非常优秀的 JavaXML API,具有性能优异、功能强大和极端易用使用的特点,同时它也是一个开放源代码的软件,可以在 SourceForge 上找到它。
- 在 IBM developerWorks 上面可以找到一篇文章,对主流的 Java XML API 进行的性能、功能和易用性 的评测,dom4j 无论在哪个方面都是非常出色的。
- 如今可以看到越来越多的 Java 软件都在使用 dom4j 来读写 XML,特别值得一提的是连 Sun 的 JAXM 也在用 dom4j。
- jar 包, Hibernate 用它来读写配置文件。
- DOM4J 官方网站:http://www.dom4j.org/
- 生成的XML文件是已格式化的
-
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
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) {
Document document=DocumentHelper.createDocument(); //获取文档
Element studentElement=document.addElement("student"); //通过文档直接添加元素
studentElement.addAttribute("id", "001"); //添加属性
studentElement.addAttribute("aa", "xx");
Element name=studentElement.addElement("name"); //添加元素
name.setText("张三"); //设置元素值
Element sex=studentElement.addElement("sex");
sex.setText("男");
Element age=studentElement.addElement("age");
age.setText("20");
OutputFormat format=OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8");
try {
XMLWriter writer=new XMLWriter(new FileOutputStream("src/student3.xml"),format); // 输出
writer.write(document);
writer.close();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
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 saxReader=new SAXReader(); //SAXReader
Document document=saxReader.read("src/students.xml");
Element rootElement=document.getRootElement();
Iterator iter=rootElement.elementIterator();
while(iter.hasNext()){
Element studentElement=(Element)iter.next();
System.out.println("学号:"+studentElement.attributeValue("id"));
System.out.println("姓名:"+studentElement.elementText("name"));
System.out.println("性别:"+studentElement.elementText("sex"));
System.out.println("年龄:"+studentElement.elementText("age"));
System.out.println("=========");
}
}
}
转载于:https://www.cnblogs.com/Cocoomg/p/9901673.html