现在的位置: 首页 > 综合 > 正文

JDOM

2013年04月07日 ⁄ 综合 ⁄ 共 3981字 ⁄ 字号 评论关闭

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使用

Java代码  收藏代码
  1. JDOMTest.java  
  2. import java.io.IOException;  
  3. import org.jdom.Document;  
  4. import org.jdom.Element;  
  5. import org.jdom.ProcessingInstruction;  
  6. import org.jdom.output.Format;  
  7. import org.jdom.output.XMLOutputter;  
  8.   
  9.   
  10. public class JDOMTest {  
  11.   
  12.       
  13.     public static void main(String[] args) {  
  14.         //1.先创建一个空的文档对象  
  15.         Document doc = new Document();  
  16.         //2.得到处理指令对象  
  17.         ProcessingInstruction pi = new ProcessingInstruction(  
  18.                 "xml-stylesheet","type='text/xsl' href='student.xsl'");  
  19.         //3.添加到文档对象中  
  20.         doc.addContent(pi);  
  21.         //4.设置根节点  
  22.         Element root = new Element("students");  
  23.         doc.setRootElement(root);  
  24.         //5.设置文本节点  
  25.         Element eltStu1 = new Element("student");  
  26.         Element eltName1 = new Element("name");  
  27.         Element eltAge1 = new Element("age");  
  28.         eltName1.setText("张三");  
  29.         eltAge1.setText("18");  
  30.         //6.将文本节点添加到元素节点<student>下  
  31.         eltStu1.addContent(eltName1);  
  32.         eltStu1.addContent(eltAge1);  
  33.         eltStu1.setAttribute("sn","01");  
  34.         //7.将<student>添加到<students>下  
  35.         root.addContent(eltStu1);  
  36.         //8.构造输出流  
  37.         XMLOutputter xmlOut = new XMLOutputter();  
  38.         //9.设置XML输出的外观  
  39.         Format fmt = Format.getPrettyFormat();  
  40.         fmt.setIndent("    ");//四个空格  
  41.         fmt.setEncoding("gb2312");  
  42.         //10.设置格式与输出相关  
  43.         xmlOut.setFormat(fmt);  
  44.         try {  
  45.             xmlOut.output(doc, System.out);  
  46.         } catch (IOException e) {  
  47.             e.printStackTrace();  
  48.         }  
  49.     }  
  50.   
  51. }  

JDOM实例二:对students.xml节点的添加/删除/修改
JDOMConvert.java

Java代码  收藏代码
  1. import java.io.File;  
  2. import java.io.IOException;  
  3. import org.jdom.Document;  
  4. import org.jdom.Element;  
  5. import org.jdom.JDOMException;  
  6. import org.jdom.input.SAXBuilder;  
  7. import org.jdom.output.Format;  
  8. import org.jdom.output.XMLOutputter;  
  9.   
  10. public class JDOMConvert {  
  11.       
  12.     public static void main(String[] args) {  
  13.         //1.构件SAXBuilder  
  14.         SAXBuilder saxBuilder = new SAXBuilder();  
  15.         try {  
  16.             //2.返回DOC对象  
  17.             Document doc = saxBuilder.build(new File("students.xml"));  
  18.             //3.构建元素对象  
  19.             Element eltStu = new Element("student");  
  20.             Element eltName = new Element("name");  
  21.             Element eltAge = new Element("age");  
  22.             //4.设置文本节点值  
  23.             eltName.setText("王五");  
  24.             eltAge.setText("11");  
  25.             //5.把名称跟年龄添加到<student>下  
  26.             eltStu.addContent(eltName);  
  27.             eltStu.addContent(eltAge);  
  28.             eltStu.setAttribute("sn","03");  
  29.             //6.构建根元素  
  30.             Element root = doc.getRootElement();  
  31.             //7.把<student>添加到根元素下  
  32.             root.addContent(eltStu);  
  33.             //删除功能  
  34.             root.removeChild("student");  
  35.             //修改功能  
  36.             root.getChild("student").getChild("age").setText("33");  
  37.             //输出  
  38.             XMLOutputter xmlOut = new XMLOutputter();  
  39.             Format fmt = Format.getPrettyFormat();  
  40.             fmt.setIndent("    ");//四个空格  
  41.             fmt.setEncoding("gb2312");  
  42.             //设置格式与输出相关  
  43.             xmlOut.setFormat(fmt);  
  44.             try {  
  45.                 xmlOut.output(doc, System.out);  
  46.             } catch (IOException e) {  
  47.                 e.printStackTrace();  
  48.             }  
  49.         } catch (JDOMException e) {  
  50.             e.printStackTrace();  
  51.         } catch (IOException e) {  
  52.             e.printStackTrace();  
  53.         }  
  54.     }  
  55.   

抱歉!评论已关闭.