xml资料保存4

第二种,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. }  
import java.io.*;
import java.util.*;
import org.dom4j.*;
import org.dom4j.io.*;

public class MyXMLReader2DOM4J {
 public static void main(String arge[]) {
  long lasting = System.currentTimeMillis();
  try {
   File f = new File("data_10k.xml");
   SAXReader reader = new SAXReader();
   Document doc = reader.read(f);
   Element root = doc.getRootElement();
   Element foo;
   for (Iterator i = root.elementIterator("VALUE"); i.hasNext();) {
    foo = (Element) i.next();
    System.out.print("车牌号码:" + foo.elementText("NO"));
    System.out.println("车主地址:" + foo.elementText("ADDR"));
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
}

第三种 JDOM实现方法:

  1. import java.io.*;   
  2. import java.util.*;   
  3. import org.jdom.*;   
  4. import org.jdom.input.*;   
  5.   
  6. public class MyXMLReader2JDOM {   
  7. public static void main(String arge[]) {   
  8.   long lasting = System.currentTimeMillis();   
  9.   try {   
  10.     SAXBuilder builder = new SAXBuilder();   
  11.     Document doc = builder.build(new File("data_10k.xml"));   
  12.     Element foo = doc.getRootElement();   
  13.     List allChildren = foo.getChildren();   
  14.    for (int i = 0; i < allChildren.size(); i++) {   
  15.      System.out.print("车牌号码:"+ ((Element) allChildren.get(i)).getChild("NO").getText());   
  16.      System.out.println("车主地址:"+ ((Element) allChildren.get(i)).getChild("ADDR").getText());   
  17.     }   
  18.    } catch (Exception e) {   
  19.     e.printStackTrace();   
  20.    }   
  21. }   
  22. }  

第四种SAX实现方法:

  1. import javax.xml.parsers.SAXParser;   
  2. import javax.xml.parsers.SAXParserFactory;   
  3.   
  4. import org.xml.sax.Attributes;   
  5. import org.xml.sax.InputSource;   
  6. import org.xml.sax.SAXException;   
  7. import org.xml.sax.helpers.DefaultHandler;   
  8.   
  9. public class MyXMLReader2SAX extends DefaultHandler {   
  10.   
  11. java.util.Stack tags = new java.util.Stack();   
  12.   
  13. public MyXMLReader2SAX() {   
  14.   super();   
  15. }   
  16.   
  17. public static void main(String args[]) {   
  18.   long lasting = System.currentTimeMillis();   
  19.   try {   
  20.     SAXParserFactory sf = SAXParserFactory.newInstance();   
  21.     SAXParser sp = sf.newSAXParser();   
  22.     MyXMLReader2SAX reader = new MyXMLReader2SAX();   
  23.     sp.parse(new InputSource("data_10k.xml"), reader);   
  24.    } catch (Exception e) {   
  25.     e.printStackTrace();   
  26.    }   
  27.   
  28.    System.out.println("运行时间:" + (System.currentTimeMillis() - lasting)   
  29.      + "毫秒");   
  30. }   
  31.   
  32. public void characters(char ch[], int start, int length)   
  33.    throws SAXException {   
  34.    String tag = (String) tags.peek();   
  35.   if (tag.equals("NO")) {   
  36.     System.out.print("车牌号码:" + new String(ch, start, length));   
  37.    }   
  38.   if (tag.equals("ADDR")) {   
  39.     System.out.println("地址:" + new String(ch, start, length));   
  40.    }   
  41. }   
  42.   
  43. public void startElement(String uri, String localName, String qName,   
  44.     Attributes attrs) {   
  45.    tags.push(qName);   
  46. }   
  47. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值