熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

Java讀取xml文件的四種方法

2022-06-13   來源: Java核心技術 

  xml文件

  Xml代碼

  <?xml version= encoding=GB?>

  <RESULT>

  <VALUE>

  <NO>A</NO>

  <ADDR>河南省鄭州市</ADDR>

  </VALUE>

  <VALUE>

  <NO>B</NO>

  <ADDR>河南省鄭州市二七區</ADDR>

  </VALUE>

  </RESULT>

  第一種 DOM 實現方法

  Java代碼

  import javaioFile;

  import javaxxmlparsersDocumentBuilder;

  import javaxxmlparsersDocumentBuilderFactory;

  import orgwcdomDocument;

  import orgwcdomNodeList;

  public class MyXMLReaderDOM {

  public static void main(String arge[]) {

  long lasting = SystemcurrentTimeMillis();

  try {

  File f = new File(data_kxml);

  DocumentBuilderFactory factory = DocumentBuilderFactorynewInstance();

  DocumentBuilder builder = factorynewDocumentBuilder();

  Document doc = builderparse(f);

  NodeList nl = docgetElementsByTagName(VALUE);

  for (int i = ; i < nlgetLength(); i++) {

  Systemoutprint(車牌號碼:+ docgetElementsByTagName(NOem(i)getFirstChild()getNodeValue());

  Systemoutprintln(車主地址:+ docgetElementsByTagName(ADDRem(i)getFirstChild()getNodeValue());

  Systemoutprintln(運行時間 + (SystemcurrentTimeMillis() lasting)

  + 毫秒);

  }

  }

  } catch (Exception e) {

  eprintStackTrace();

  }

  }

  }

  第二種DOMJ實現方法

  Java代碼

  import javaio*;

  import javautil*;

  import orgdomj*;

  import orgdomjio*;

  public class MyXMLReaderDOMJ {

  public static void main(String arge[]) {

  long lasting = SystemcurrentTimeMillis();

  try {

  File f = new File(data_kxml);

  SAXReader reader = new SAXReader();

  Document doc = readerread(f);

  Element root = docgetRootElement();

  Element foo;

  for (Iterator i = rootelementIterator(VALUE); ihasNext();) {

  foo = (Element) inext();

  Systemoutprint(車牌號碼: + fooelementText(NO));

  Systemoutprintln(車主地址: + fooelementText(ADDR));

  }

  Systemoutprintln(運行時間 + (SystemcurrentTimeMillis() lasting)

  + 毫秒);

  }

  } catch (Exception e) {

  eprintStackTrace();

  }

  }

  }

  第三種 JDOM實現方法

  Java代碼

  import javaio*;

  import javautil*;

  import orgjdom*;

  import orgjdominput*;

  public class MyXMLReaderJDOM {

  public static void main(String arge[]) {

  long lasting = SystemcurrentTimeMillis();

  try {

  SAXBuilder builder = new SAXBuilder();

  Document doc = builderbuild(new File(data_kxml));

  Element foo = docgetRootElement();

  List allChildren = foogetChildren();

  for (int i = ; i < allChildrensize(); i++) {

  Systemoutprint(車牌號碼:+ ((Element) allChildrenget(i))getChild(NO)getText());

  Systemoutprintln(車主地址:+ ((Element) allChildrenget(i))getChild(ADDR)getText());

  }

  Systemoutprintln(運行時間 + (SystemcurrentTimeMillis() lasting)

  + 毫秒);

  }

  } catch (Exception e) {

  eprintStackTrace();

  }

  }

  }

  第四種SAX實現方法

  Java代碼

  import javaxxmlparsersSAXParser;

  import javaxxmlparsersSAXParserFactory;

  import orgxmlsaxAttributes;

  import orgxmlsaxInputSource;

  import orgxmlsaxSAXException;

  import orgxmlsaxhelpersDefaultHandler;

  public class MyXMLReaderSAX extends DefaultHandler {

  javautilStack tags = new javautilStack();

  public MyXMLReaderSAX() {

  super();

  }

  public static void main(String args[]) {

  long lasting = SystemcurrentTimeMillis();

  try {

  SAXParserFactory sf = SAXParserFactorynewInstance();

  SAXParser sp = sfnewSAXParser();

  MyXMLReaderSAX reader = new MyXMLReaderSAX();

  spparse(new InputSource(data_kxml) reader);

  } catch (Exception e) {

  eprintStackTrace();

  }

  Systemoutprintln(運行時間 + (SystemcurrentTimeMillis() lasting)

  + 毫秒);

  }

  public void characters(char ch[] int start int length)

  throws SAXException {

  String tag = (String) tagspeek();

  if (tagequals(NO)) {

  Systemoutprint(車牌號碼 + new String(ch start length));

  }

  if (tagequals(ADDR)) {

  Systemoutprintln(地址: + new String(ch start length));

  }

  }

  public void startElement(String uri String localName String qName

  Attributes attrs) {

  tagspush(qName);

  }

  }


From:http://tw.wingwit.com/Article/program/Java/hx/201311/25610.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.