viita技术空间
viita_boy
全部博文(114)
2011年(2)
2009年(1)
2008年(111)
Anne_zyt
adminse
wangzi20
14332412
weixiaof
cqxfy
stevenfu
10161350
qq_20765
分类: Java
2008-08-17 20:26:34
package com.viita; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.List; 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 TestJdom<E> { //读取xml文件 public void read() throws Exception { // TODO Auto-generated method stub SAXBuilder sd = new SAXBuilder(); Document doc = sd.build("c:/db.xml"); Element root = doc.getRootElement(); System.out.println(root.getChild("branch").getValue()); //读取指定节点名称的值 //循环读取所有节点信息 List<Element> list = root.getChildren(); for (Element element : list) { System.out.println(element.getName()+" "+element.getValue()); } } //写入xml文件 public void write() throws Exception { // TODO Auto-generated method stub Document doc = new Document(); Element root = new Element("root"); //添加节点 Element leafage = new Element("leafage"); leafage.setText("left"); //给节点赋值 Element branch = new Element("branch"); branch.setText("right"); //给节点赋值 root.addContent(leafage); root.addContent(branch); //子结点与根节点进行关联 doc.setRootElement(root);//确定根节点 //输出 XMLOutputter out = new XMLOutputter(); Format format = Format.getPrettyFormat(); out.setFormat(format); out.output(doc,new FileOutputStream("c:/db.xml")); // out.output(doc,System.out); } public static void main(String[] args) throws Exception { TestJdom<Integer> jdm = new TestJdom<Integer>(); jdm.read(); // jdm.write(); } public void setdoX(E e) { } }
上一篇:面向对象的特别理解
下一篇:DOM4J操作
登录 注册