java读取xml文件的四种方法

编程技术  /  houtizong 发布于 3年前   73
xml文件:

Xml代码
<?xml version="1.0" encoding="GB2312"?> 
<RESULT> 
<VALUE>    
  <NO>A1234</NO>    
  <ADDR>河南省郑州市</ADDR> 
</VALUE> 
<VALUE>    
  <NO>B1234</NO>    
  <ADDR>河南省郑州市二七区</ADDR> 
</VALUE> 
</RESULT>  

<?xml version="1.0" encoding="GB2312"?>
<RESULT>
<VALUE>  
  <NO>A1234</NO>  
  <ADDR>河南省郑州市</ADDR>
</VALUE>
<VALUE>  
  <NO>B1234</NO>  
  <ADDR>河南省郑州市二七区</ADDR>
</VALUE>
</RESULT>
第一种 DOM 实现方法:

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();  
  }  
}  


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实现方法:

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("车牌号码:" + foo.elementText("NO"));  
    System.out.println("车主地址:" + foo.elementText("ADDR"));  
   }  
  } catch (Exception e) {  
   e.printStackTrace();  
  }  
}  


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实现方法:

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("车牌号码:"+ ((Element) allChildren.get(i)).getChild("NO").getText());  
    System.out.println("车主地址:"+ ((Element) allChildren.get(i)).getChild("ADDR").getText());  
   }  
  } catch (Exception e) {  
   e.printStackTrace();  
  }  
}  


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实现方法:

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("运行时间:" + (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);  
}  

上一篇:页面打开EXE
下一篇:java 时间处理

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!

留言需要登陆哦

技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成

网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];

      订阅博客周刊 去订阅

文章归档

文章标签

友情链接

Auther ·HouTiZong
侯体宗的博客
© 2020 zongscan.com
版权所有ICP证 : 粤ICP备20027696号
PHP交流群 也可以扫右边的二维码
侯体宗的博客