dom4j test

java 代码
  1. package dom4j;   
  2.   
  3. import java.io.FileWriter;   
  4. import java.io.IOException;   
  5. import java.util.Iterator;   
  6. import java.util.List;   
  7.   
  8. import org.dom4j.*;   
  9. import org.dom4j.io.*;   
  10.   
  11. public class Dom4jTest {   
  12.   
  13.     /**  
  14.      * @param args  
  15.      * @throws IOException  
  16.      */  
  17.     public static void main(String[] args) throws Exception {   
  18.         // TODO Auto-generated method stub   
  19.   
  20.         // Document doc = createDocument();   
  21.         // write(doc);   
  22.         SAXReader sr = new SAXReader();   
  23.         Document pdoc = sr.read("output.xml");   
  24.   
  25.         List<Element> list = pdoc.getRootElement().elements();   
  26.         List<Attribute> alist;   
  27.         for (Element element : list) {   
  28.             System.out.println(element.getName() + " : " + element.getText());   
  29.             // if(element.getName().equalsIgnoreCase("title"))   
  30.             element.addAttribute("price""$1000");   
  31.             System.out.println("attribute: " + element.attribute("name").getData());   
  32.             alist = element.attributes();   
  33.             for (Attribute att : alist)   
  34.                 System.out.println(att.getName() + " : " + att.getText());   
  35.         }   
  36.         System.out.println();   
  37.         for (Iterator iter = pdoc.getRootElement().elementIterator(); iter   
  38.                 .hasNext();) {   
  39.             Element element = (Element) iter.next();   
  40.             System.out.println(element.getName() + " : " + element.getText());   
  41.   
  42.         }   
  43.         write(pdoc);   
  44.   
  45.     }   
  46.   
  47.     public static Document createDocument() {   
  48.         Document document = DocumentHelper.createDocument();   
  49.         Element root = document.addElement("root");   
  50.   
  51.         Element author1 = root.addElement("author").addAttribute("name",   
  52.                 "James").addAttribute("location""UK").addText(   
  53.                 "James Strachan");   
  54.   
  55.         Element author2 = root.addElement("author").addAttribute("name""Bob")   
  56.                 .addAttribute("location""US").addText("Bob McWhirter");   
  57.   
  58.         return document;   
  59.     }   
  60.   
  61.     public static void write(Document document) throws IOException {   
  62.   
  63.         // 写入文件   
  64.         XMLWriter writer = new XMLWriter(new FileWriter("output.xml"),   
  65.                 OutputFormat.createPrettyPrint());   
  66.         writer.write(document);   
  67.         writer.close();   
  68.   
  69.         // 以一种优雅的格式写入System.out对象   
  70.         OutputFormat format = OutputFormat.createPrettyPrint();   
  71.         writer = new XMLWriter(System.out, format);   
  72.         writer.write(document);   
  73.   
  74.         // 以一种紧凑的格式写入System.out对象   
  75.         format = OutputFormat.createCompactFormat();   
  76.         writer = new XMLWriter(System.out, format);   
  77.         writer.write(document);   
  78.     }   
  79.   
  80. }   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值