精----Java读取xml文件的四种方法
xml文件:
Xml代码
<?xml version="1.0" encoding="GB2312"?> <RESULT> <VALUE> <NO>A1234</NO> <ADDR>河南省郑州市</ADDR> </VALUE> <VALUE> <NO>B1234</NO> <ADDR>河南省郑州市二七区</ADDR> </VALUE> </RESULT>
第一种 DOM 实现方法:
Java代码
1 import java.io.File; 2 import javax.xml.parsers.DocumentBuilder; 3 import javax.xml.parsers.DocumentBuilderFactory; 4 import org.w3c.dom.Document; 5 import org.w3c.dom.NodeList; 6 public class MyXMLReader2DOM { 7 public static void main(String arge[]) { 8 long lasting = System.currentTimeMillis(); 9 try { 10 File f = new File("data_10k.xml"); 11 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 12 DocumentBuilder builder = factory.newDocumentBuilder(); 13 Document doc = builder.parse(f); 14 NodeList nl = doc.getElementsByTagName("VALUE"); 15 for (int i = 0; i < nl.getLength(); i++) { 16 System.out.print("车牌号码:"+ doc.getElementsByTagName("NO").item(i).getFirstChild().getNodeValue()); 17 System.out.println("车主地址:"+ doc.getElementsByTagName("ADDR").item(i).getFirstChild().getNodeValue()); 18 System.out.println("运行时间:" + (System.currentTimeMillis() - lasting) 19 + "毫秒"); 20 } 21 } 22 } catch (Exception e) { 23 e.printStackTrace(); 24 } 25 } 26 }
|
第二种,DOM4J实现方法:
Java代码
1 import java.io.*; 2 import java.util.*; 3 import org.dom4j.*; 4 import org.dom4j.io.*; 5 public class MyXMLReader2DOM4J { 6 public static void main(String arge[]) { 7 long lasting = System.currentTimeMillis(); 8 try { 9 File f = new File("data_10k.xml"); 10 SAXReader reader = new SAXReader(); 11 Document doc = reader.read(f); 12 Element root = doc.getRootElement(); 13 Element foo; 14 for (Iterator i = root.elementIterator("VALUE"); i.hasNext();) { 15 foo = (Element) i.next(); 16 System.out.print("车牌号码:" + foo.elementText("NO")); 17 System.out.println("车主地址:" + foo.elementText("ADDR")); 18 } 19 System.out.println("运行时间:" + (System.currentTimeMillis() - lasting) 20 + "毫秒"); 21 } 22 } catch (Exception e) { 23 e.printStackTrace(); 24 } 25 } 26 }
|
第三种 JDOM实现方法:
Java代码
1 import java.io.*; 2 import java.util.*; 3 import org.jdom.*; 4 import org.jdom.input.*; 5 public class MyXMLReader2JDOM { 6 public static void main(String arge[]) { 7 long lasting = System.currentTimeMillis(); 8 try { 9 SAXBuilder builder = new SAXBuilder(); 10 Document doc = builder.build(new File("data_10k.xml")); 11 Element foo = doc.getRootElement(); 12 List allChildren = foo.getChildren(); 13 for (int i = 0; i < allChildren.size(); i++) { 14 System.out.print("车牌号码:"+ ((Element) allChildren.get(i)).getChild("NO").getText()); 15 System.out.println("车主地址:"+ ((Element) allChildren.get(i)).getChild("ADDR").getText()); 16 } 17 System.out.println("运行时间:" + (System.currentTimeMillis() - lasting) 18 + "毫秒"); 19 } 20 } catch (Exception e) { 21 e.printStackTrace(); 22 } 23 } 24 }
|
第四种SAX实现方法:
Java代码
1 import javax.xml.parsers.SAXParser; 2 import javax.xml.parsers.SAXParserFactory; 3 import org.xml.sax.Attributes; 4 import org.xml.sax.InputSource; 5 import org.xml.sax.SAXException; 6 import org.xml.sax.helpers.DefaultHandler; 7 public class MyXMLReader2SAX extends DefaultHandler { 8 java.util.Stack tags = new java.util.Stack(); 9 public MyXMLReader2SAX() { 10 super(); 11 } 12 public static void main(String args[]) { 13 long lasting = System.currentTimeMillis(); 14 try { 15 SAXParserFactory sf = SAXParserFactory.newInstance(); 16 SAXParser sp = sf.newSAXParser(); 17 MyXMLReader2SAX reader = new MyXMLReader2SAX(); 18 sp.parse(new InputSource("data_10k.xml"), reader); 19 } catch (Exception e) { 20 e.printStackTrace(); 21 } 22 System.out.println("运行时间:" + (System.currentTimeMillis() - lasting) 23 + "毫秒"); 24 } 25 public void characters(char ch[], int start, int length) 26 throws SAXException { 27 String tag = (String) tags.peek(); 28 if (tag.equals("NO")) { 29 System.out.print("车牌号码:" + new String(ch, start, length)); 30 } 31 if (tag.equals("ADDR")) { 32 System.out.println("地址:" + new String(ch, start, length)); 33 } 34 } 35 public void startElement(String uri, String localName, String qName, 36 Attributes attrs) { 37 tags.push(qName); 38 } 39 }
|