标签:
http://blog.csdn.net/ahangliu/article/details/1533127
test1.xml 文件内容
<?xml version="1.0" encoding="GB2312"?> <RESULT> <VALUE> <NO>A1234</NO> <ADDR>河南省郑州市</ADDR> </VALUE> <VALUE> <NO>B1234</NO> <ADDR>河南省郑州市二七区</ADDR> </VALUE> </RESULT>第一种 DOM 实现方法: DOM是基于XML文档树结构的解析
<pre name="code" class="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("车牌号码:" + doc.getElementsByTagName("NO").item(i) .getFirstChild().getNodeValue()); System.out.println("车主地址:" + doc.getElementsByTagName("ADDR").item(i) .getFirstChild().getNodeValue()); } } catch (Exception e) { e.printStackTrace(); } } }
第二种方法 DOM4J
DOM4J 是一个非常非常优秀的Java XML API,具有性能优异、功能强大和极端易用使用的特点,同时它也是一个开放源代码的软件。如今你可以看到越来越多的 Java 软件都在使用 DOM4J 来读写 XML,特别值得一提的是连 Sun 的 JAXM 也在用 DOM4J。
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(); } } }
为减少DOM、SAX的编码量,出现了JDOM;优点:20-80原则,极大减少了代码量。使用场合:要实现的功能简单,如解析、创建等,但在底层,JDOM还是使用SAX(最常用)、DOM、Xanan文档。
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(); } } }
为解决DOM的问题,出现了SAX。SAX ,事件驱动。当解析器发现元素开始、元素结束、文本、文档的开始或结束等时,发送事件,程序员编写响应这些事件的代码,保存数据。优点:不用事先调入整个文档,占用资源少;SAX解析器代码比DOM解析器代码小,适于Applet,下载。缺点:不是持久的;事件过后,若没保存数据,那么数据就丢了;无状态性;从事件中只能得到文本,但不知该文本属于哪个元素;使用场合:Applet;只需XML文档的少量内容,很少回头访问;机器内存少;
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); } }
标签:
原文地址:http://blog.csdn.net/bobozhanghb/article/details/51356146