JDOM

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使用
[code]
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();
}
}

}
[/code]
JDOM实例二:对students.xml节点的添加/删除/修改
JDOMConvert.java
[code]
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();
}
}

}
[/code]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值