java讀取xml檔案的四種方法

來源:互聯網
上載者:User

xml檔案

<?xml version="1.0" encoding="GB2312"?>
<RESULT>
 <VALUE>
    
  <NO>A1234</NO>
    
  <ADDR>四川省XX縣XX鎮XX路X段XX號</ADDR>
 </VALUE>
 <VALUE>
    
  <NO>B1234</NO>
     
  <ADDR>四川省XX市XX鄉XX村XX組</ADDR>
 </VALUE>
</RESULT> 

第一種 DOM 實現方法

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

public class MyXMLReader2DOM {
 public static void main(String arge[]) {

  long lasting = System.currentTimeMillis();
 
  try {
   File f = new File("data_10k.xml");
   DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
   DocumentBuilder builder = factory.newDocumentBuilder();
   Document doc = builder.parse(f);
   NodeList nl = doc.getElementsByTagName("VALUE");
   for (int i = 0; i < nl.getLength(); i++) {
    System.out.print("車牌號碼:"+ doc.getElementsByTagName("NO").item(i).getFirstChild().getNodeValue());
    System.out.println("車主地址:"+ doc.getElementsByTagName("ADDR").item(i).getFirstChild().getNodeValue());
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
}
第二種,DOM4J實現方法

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實現方法

import java.io.*;
import java.util.*;
import org.jdom.*;
import org.jdom.input.*;

public class MyXMLReader2JDOM {
 public static void main(String arge[]) {
  long lasting = System.currentTimeMillis();
  try {
   SAXBuilder builder = new SAXBuilder();
   Document doc = builder.build(new File("data_10k.xml"));
   Element foo = doc.getRootElement();
   List allChildren = foo.getChildren();
   for (int i = 0; i < allChildren.size(); i++) {
    System.out.print("車牌號碼:"+ ((Element) allChildren.get(i)).getChild("NO").getText());
    System.out.println("車主地址:"+ ((Element) allChildren.get(i)).getChild("ADDR").getText());
   }
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
}

第四種SAX實現方法

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class MyXMLReader2SAX extends DefaultHandler {

 java.util.Stack tags = new java.util.Stack();

 public MyXMLReader2SAX() {
  super();
 }

 public static void main(String args[]) {
  long lasting = System.currentTimeMillis();
  try {
   SAXParserFactory sf = SAXParserFactory.newInstance();
   SAXParser sp = sf.newSAXParser();
   MyXMLReader2SAX reader = new MyXMLReader2SAX();
   sp.parse(new InputSource("data_10k.xml"), reader);
  } catch (Exception e) {
   e.printStackTrace();
  }

  System.out.println("已耗用時間:" + (System.currentTimeMillis() - lasting)
    + "毫秒");
 }

 public void characters(char ch[], int start, int length)
   throws SAXException {
  String tag = (String) tags.peek();
  if (tag.equals("NO")) {
   System.out.print("車牌號碼:" + new String(ch, start, length));
  }
  if (tag.equals("ADDR")) {
   System.out.println("地址:" + new String(ch, start, length));
  }
 }

 public void startElement(String uri, String localName, String qName,
   Attributes attrs) {
  tags.push(qName);
 }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.