久久久久无码精品,四川省少妇一级毛片,老老熟妇xxxxhd,人妻无码少妇一区二区

Java讀取xml文件的方法

時(shí)間:2024-07-20 23:59:49 SUN認(rèn)證 我要投稿
  • 相關(guān)推薦

Java讀取xml文件的方法

  xml文件:

  Xml代碼

  A1234

  XX省XX市

  B1234

  XX省XX市二七區(qū)

  第一種 DOM 實(shí)現(xiàn)方法:

  Java代碼

  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("車牌號(hào)碼:"+ doc.getElementsByTagName("NO").item(i).getFirstChild().getNodeValue());

  System.out.println("車主地址:"+ doc.getElementsByTagName("ADDR").item(i).getFirstChild().getNodeValue());

  System.out.println("運(yùn)行時(shí)間:" + (System.currentTimeMillis() - lasting)

  + "毫秒");

  }

  }

  } catch (Exception e) {

  e.printStackTrace();

  }

  }

  }

  第二種,DOM4J實(shí)現(xiàn)方法:

  Java代碼

  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("車牌號(hào)碼:" + foo.elementText("NO"));

  System.out.println("車主地址:" + foo.elementText("ADDR"));

  }

  System.out.println("運(yùn)行時(shí)間:" + (System.currentTimeMillis() - lasting)

  + "毫秒");

  }

  } catch (Exception e) {

  e.printStackTrace();

  }

  }

  }

  第三種 JDOM實(shí)現(xiàn)方法:

  Java代碼

  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("車牌號(hào)碼:"+ ((Element) allChildren.get(i)).getChild("NO").getText());

  System.out.println("車主地址:"+ ((Element) allChildren.get(i)).getChild("ADDR").getText());

  }

  System.out.println("運(yùn)行時(shí)間:" + (System.currentTimeMillis() - lasting)

  + "毫秒");

  }

  } catch (Exception e) {

  e.printStackTrace();

  }

  }

  }

  第四種SAX實(shí)現(xiàn)方法:

  Java代碼

  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("運(yùn)行時(shí)間:" + (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("車牌號(hào)碼:" + 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);

  }

  }

【Java讀取xml文件的方法】相關(guān)文章:

Android讀取本地json文件的方法07-06

怎么打開xml文件的方法和軟件06-12

Android XML文件中的08-31

使用XQEngine來搜索XML文件內(nèi)容07-07

用EXCEL表格和軟件打開xml文件的方法具體步驟介紹07-22

Java中如何構(gòu)造、生成XML簡明07-17

用Java如何處理XML數(shù)據(jù)10-22

實(shí)現(xiàn)xml文件解析三種方式10-14

java操作xml的一個(gè)小例子09-06

Java文件解壓縮示例08-21