xml资料保存4

来源:互联网 发布:mac 预览 pdf 翻页 编辑:程序博客网 时间:2024/05/22 15:52

第二种,DOM4J实现方法:

  1. import java.io.*;   
  2. import java.util.*;   
  3. import org.dom4j.*;   
  4. import org.dom4j.io.*;   
  5.   
  6. public class MyXMLReader2DOM4J {   
  7. public static void main(String arge[]) {   
  8.   long lasting = System.currentTimeMillis();   
  9.   try {   
  10.     File f = new File("data_10k.xml");   
  11.     SAXReader reader = new SAXReader();   
  12.     Document doc = reader.read(f);   
  13.     Element root = doc.getRootElement();   
  14.     Element foo;   
  15.    for (Iterator i = root.elementIterator("VALUE"); i.hasNext();) {   
  16.      foo = (Element) i.next();   
  17.      System.out.print("车牌号码:" + foo.elementText("NO"));   
  18.      System.out.println("车主地址:" + foo.elementText("ADDR"));   
  19.     }   
  20.    } catch (Exception e) {   
  21.     e.printStackTrace();   
  22.    }   
  23. }   
  24. }