必威体育Betway必威体育官网
当前位置:首页 > IT技术

Java解析XML文件的四种方式

时间:2019-08-05 17:43:11来源:IT技术作者:seo实验室小编阅读:83次「手机版」
 

解析xml

java读取xml文件的四种方法

Xml代码

复制代码

 1 <?xml version="1.0" encoding="gb2312"?>  
 2 <RESULT>  
 3 <VALUE>     
 4   <NO>A1234</NO>     
 5   <ADDR>河南省郑州市</ADDR>  
 6 </VALUE>  
 7 <VALUE>     
 8   <NO>B1234</NO>     
 9   <ADDR>河南省郑州市二七区</ADDR>  
10 </VALUE>  
11 </RESULT>

复制代码

第一种 DOM 实现方法:

复制代码

 1 import java.io.File;   
 2   
 3 import javax.xml.parsers.Documentbuilder;   
 4 import javax.xml.parsers.DocumentBuilderFactory;   
 5   
 6 import org.w3c.dom.Document;   
 7 import org.w3c.dom.NodeList;   
 8   
 9 public class MyXMLReader2DOM {   
10 public static void main(String arge[]) {   
11   
12   long lasting = system.currenttimemillis();   
13   
14   try {   
15     File f = new File("data_10k.xml");   
16     DocumentBuilderFactory factory = DocumentBuilderFactory.newinstance();   
17     DocumentBuilder builder = factory.newDocumentBuilder();   
18     Document doc = builder.parse(f);   
19     NodeList nl = doc.getElementsByTagName_r("VALUE");   
20    for (int i = 0; i < nl.getLength(); i++) {   
21      System.out.print("车牌号码:"+ doc.getElementsByTagName_r("NO").item(i).getFirstChild().getNodeValue());   
22      System.out.println("车主地址:"+ doc.getElementsByTagName_r("ADDR").item(i).getFirstChild().getNodeValue());   
23     }   
24    } catch (Exception e) {   
25     e.printstacktrace();   
26    }   
27 }   
28 }  

复制代码

第二种,DOM4J实现方法

复制代码

 1 import java.io.*;   
 2 import java.util.*;   
 3 import org.dom4j.*;   
 4 import org.dom4j.io.*;   
 5   
 6 public class MyXMLReader2DOM4J {   
 7 public static void main(String arge[]) {   
 8   long lasting = System.currentTimeMillis();   
 9   try {   
10     File f = new File("data_10k.xml");   
11     SAXReader reader = new SAXReader();   
12     Document doc = reader.read(f);   
13     Element root = doc.getRootElement();   
14     Element foo;   
15    for (Iterator i = root.elementIterator("VALUE"); i.hasNext();) {   
16      foo = (Element) i.next();   
17      System.out.print("车牌号码:" + foo.elementText("NO"));   
18      System.out.println("车主地址:" + foo.elementText("ADDR"));   
19     }   
20    } catch (Exception e) {   
21     e.printStackTrace();   
22    }   
23 }   
24 }  

复制代码

第三种 JDOM实现方法:

复制代码

 1 import java.io.*;   
 2 import java.util.*;   
 3 import org.jdom.*;   
 4 import org.jdom.input.*;   
 5   
 6 public class MyXMLReader2JDOM {   
 7 public static void main(String arge[]) {   
 8   long lasting = System.currentTimeMillis();   
 9   try {   
10     SAXBuilder builder = new SAXBuilder();   
11     Document doc = builder.build(new File("data_10k.xml"));   
12     Element foo = doc.getRootElement();   
13     List allchildren = foo.getChildren();   
14    for (int i = 0; i < allChildren.size(); i++) {   
15      System.out.print("车牌号码:"+ ((Element) allChildren.get(i)).getChild("NO").getText());   
16      System.out.println("车主地址:"+ ((Element) allChildren.get(i)).getChild("ADDR").getText());   
17     }   
18    } catch (Exception e) {   
19     e.printStackTrace();   
20    }   
21 }   
22 }

复制代码

第四种SAX实现方法:

复制代码

 1 import javax.xml.parsers.SAXParser;   
 2 import javax.xml.parsers.SAXParserFactory;   
 3   
 4 import org.xml.sax.Attributes;   
 5 import org.xml.sax.InputSource;   
 6 import org.xml.sax.SAXException;   
 7 import org.xml.sax.helpers.Defaulthandler;   
 8   
 9 public class MyXMLReader2SAX extends DefaultHandler {   
10   
11 java.util.Stack tags = new java.util.Stack();   
12   
13 public MyXMLReader2SAX() {   
14   super();   
15 }   
16   
17 public static void main(String args[]) {   
18   long lasting = System.currentTimeMillis();   
19   try {   
20     SAXParserFactory sf = SAXParserFactory.newInstance();   
21     SAXParser sp = sf.newSAXParser();   
22     MyXMLReader2SAX reader = new MyXMLReader2SAX();   
23     sp.parse(new InputSource("data_10k.xml"), reader);   
24    } catch (Exception e) {   
25     e.printStackTrace();   
26    }   
27   
28    System.out.println("运行时间:" + (System.currentTimeMillis() - lasting)   
29      + "毫秒");   
30 }   
31   
32 public void characters(char ch[], int start, int length)   
33    throws SAXException {   
34    String tag = (String) tags.peek();   
35   if (tag.equals("NO")) {   
36     System.out.print("车牌号码:" + new String(ch, start, length));   
37    }   
38   if (tag.equals("ADDR")) {   
39     System.out.println("地址:" + new String(ch, start, length));   
40    }   
41 }   
42   
43 public void startElement(String uri, String localName, String qName,   
44     Attributes attrs) {   
45    tags.push(qName);   
46 }   
47 }

相关阅读

Java堆溢出及栈溢出

内存溢出Out Of Memory(OOM):指申请内存时,没有足够的内存供其使用。 内存泄露Memory Leak:内存泄露,程序申请内存后,无法释放已申请的

Java 集合中关于Iterator 和ListIterator的详解

1.IteratorIterator的定义如下:public interface Iterator<E> {}Iterator是一个接口,它是集合的迭代器。集合可以通过Iterator去遍

Java7和Java8的自由切换(Win10)

记录在电脑中同时安装java7和java8的过程 1.下载并安装jdk1.7 和jdk1.8百度找资源或者直接官网下载: https://www.oracle.com/tech

jedis : NoSuchMethodError: org.springframework.uti

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'connectionFactory' defined in c

大数据时代,我们应该具有怎样的思维方式?

《飘》、《汤姆叔叔的小屋》之类的小说和故事在全社会激起的是强烈的“情感”共鸣,和故事相比,数据没有情节,它仅代表客观事实,数据激

分享到:

栏目导航

推荐阅读

热门阅读