JDOM(Java Document Object Model):java文档对象模型.http://www.jdom.org
DOM被设计为用于完成几乎所有的XML操作任务,同时它又是与语言无关,这就导致DOM的API庞大而复杂。为了给JAVA程序员提供一套简单易用的操作XML的API,java技术专家Jason Hunter和Brett McLaughlin创建了JDOM。
JDOM利用Java语言的优秀特性,包括方法重载,集合,反射以及JAVA程序员熟悉的编程风格,极大的简化了对XML文档的处理。
和DOM类似,JDOM也使用对象树来表示XML文档:
<name>张三</name>
要想得到“张三”这个文本节点,在DOM中:
String content = element.getFirstChild().getNodeValue();
在JDOM中:
String content = element.getText();
注意:JDOM使用SAX解析器(例如Apache的Xerces),默认JDOM使用JAXP来选择解析器。JDOM还可以接收W3C DOM格式的内容,同时,JDOM也提供了从JDOM树到SAX事件流或W3C DOM树的输出机制。
JDOM实例一:JDOM的API使用
- JDOMTest.java
- import java.io.IOException;
- import org.jdom.Document;
- import org.jdom.Element;
- import org.jdom.ProcessingInstruction;
- import org.jdom.output.Format;
- import org.jdom.output.XMLOutputter;
- public class JDOMTest {
- public static void main(String[] args) {
- //1.先创建一个空的文档对象
- Document doc = new Document();
- //2.得到处理指令对象
- ProcessingInstruction pi = new ProcessingInstruction(
- "xml-stylesheet","type='text/xsl' href='student.xsl'");
- //3.添加到文档对象中
- doc.addContent(pi);
- //4.设置根节点
- Element root = new Element("students");
- doc.setRootElement(root);
- //5.设置文本节点
- Element eltStu1 = new Element("student");
- Element eltName1 = new Element("name");
- Element eltAge1 = new Element("age");
- eltName1.setText("张三");
- eltAge1.setText("18");
- //6.将文本节点添加到元素节点<student>下
- eltStu1.addContent(eltName1);
- eltStu1.addContent(eltAge1);
- eltStu1.setAttribute("sn","01");
- //7.将<student>添加到<students>下
- root.addContent(eltStu1);
- //8.构造输出流
- XMLOutputter xmlOut = new XMLOutputter();
- //9.设置XML输出的外观
- Format fmt = Format.getPrettyFormat();
- fmt.setIndent(" ");//四个空格
- fmt.setEncoding("gb2312");
- //10.设置格式与输出相关
- xmlOut.setFormat(fmt);
- try {
- xmlOut.output(doc, System.out);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
JDOM实例二:对students.xml节点的添加/删除/修改
JDOMConvert.java
- import java.io.File;
- import java.io.IOException;
- import org.jdom.Document;
- import org.jdom.Element;
- import org.jdom.JDOMException;
- import org.jdom.input.SAXBuilder;
- import org.jdom.output.Format;
- import org.jdom.output.XMLOutputter;
- public class JDOMConvert {
- public static void main(String[] args) {
- //1.构件SAXBuilder
- SAXBuilder saxBuilder = new SAXBuilder();
- try {
- //2.返回DOC对象
- Document doc = saxBuilder.build(new File("students.xml"));
- //3.构建元素对象
- Element eltStu = new Element("student");
- Element eltName = new Element("name");
- Element eltAge = new Element("age");
- //4.设置文本节点值
- eltName.setText("王五");
- eltAge.setText("11");
- //5.把名称跟年龄添加到<student>下
- eltStu.addContent(eltName);
- eltStu.addContent(eltAge);
- eltStu.setAttribute("sn","03");
- //6.构建根元素
- Element root = doc.getRootElement();
- //7.把<student>添加到根元素下
- root.addContent(eltStu);
- //删除功能
- root.removeChild("student");
- //修改功能
- root.getChild("student").getChild("age").setText("33");
- //输出
- XMLOutputter xmlOut = new XMLOutputter();
- Format fmt = Format.getPrettyFormat();
- fmt.setIndent(" ");//四个空格
- fmt.setEncoding("gb2312");
- //设置格式与输出相关
- xmlOut.setFormat(fmt);
- try {
- xmlOut.output(doc, System.out);
- } catch (IOException e) {
- e.printStackTrace();
- }
- } catch (JDOMException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }