java如何读取xml文件 Java读取xml文件的四种方法
Java读取xml文件的四种方法
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 java io File;
import javax xml parsers DocumentBuilder;
import javax xml parsers DocumentBuilderFactory;
import w c dom Document;
import w c dom NodeList;
public class MyXMLReader DOM {
public static void main(String arge[]) {
long lasting = System currentTimeMillis();
try {
File f = new File( data_ k xml );
DocumentBuilderFactory factory = DocumentBuilderFactory newInstance();
DocumentBuilder builder = factory newDocumentBuilder();
Document doc = builder parse(f);
NodeList nl = doc getElementsByTagName( VALUE );
for (int i = ; i < nl getLength(); i++) {
System out print( 车牌号码: + doc getElementsByTagName( NO em(i) getFirstChild() getNodeValue());
System out println( 车主地址: + doc getElementsByTagName( ADDR em(i) getFirstChild() getNodeValue());
System out println( 运行时间 + (System currentTimeMillis() lasting)
+ 毫秒 );
}
}
} catch (Exception e) {
e printStackTrace();
}
}
}
第二种 DOM J实现方法
Java代码
import java io *;
import java util *;
import dom j *;
import dom j io *;
public class MyXMLReader DOM J {
public static void main(String arge[]) {
long lasting = System currentTimeMillis();
try {
File f = new File( data_ k 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 ));
}
System out println( 运行时间 + (System currentTimeMillis() lasting)
+ 毫秒 );

}
} catch (Exception e) {
e printStackTrace();
}
}
}
第三种 JDOM实现方法
Java代码
import java io *;
import java util *;
import jdom *;
import jdom input *;
public class MyXMLReader JDOM {
public static void main(String arge[]) {
long lasting = System currentTimeMillis();
try {
SAXBuilder builder = new SAXBuilder();
Document doc = builder build(new File( data_ k xml ));
Element foo = doc getRootElement();
List allChildren = foo getChildren();
for (int i = ; i < allChildren size(); i++) {
System out print( 车牌号码: + ((Element) allChildren get(i)) getChild( NO ) getText());
System out println( 车主地址: + ((Element) allChildren get(i)) getChild( ADDR ) getText());
}
System out println( 运行时间 + (System currentTimeMillis() lasting)
+ 毫秒 );
}
} catch (Exception e) {
e printStackTrace();
}
}
}
第四种SAX实现方法
Java代码
import javax xml parsers SAXParser;
import javax xml parsers SAXParserFactory;
import xml sax Attributes;
import xml sax InputSource;
import xml sax SAXException;
import xml sax helpers DefaultHandler;
public class MyXMLReader SAX extends DefaultHandler {
java util Stack tags = new java util Stack();
public MyXMLReader SAX() {
super();
}
public static void main(String args[]) {
long lasting = System currentTimeMillis();
try {
SAXParserFactory sf = SAXParserFactory newInstance();
SAXParser sp = sf newSAXParser();
MyXMLReader SAX reader = new MyXMLReader SAX();
sp parse(new InputSource( data_ k 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);
}
lishixinzhi/Article/program/Java/hx/201311/25610相关文章
- Java读取网络文件 PHP网络开发详解:HTML文件的读取[1]
- 防止xml注入的方法 xmlhttp的open方法使用详解
- 根据xml实例编写外部dtd文档 编写xml档案用什么程式编写软体好
- java读取xml配置文件 读取扩展名为xml的资源文件的方法
- java如何获取文件内容 如何在java中去掉文本文件中的“”
- 读取网络流的java开源实现 PHP网络开发详解:XML文件的读取[1]
- java的图片io流读取 java io读取文本文件
- 指定路径下没有xml单据文件 JAVA读取xml文件中节点值
- 读写文件权限怎么开 JAVA文件操作类,方便读写小文件
- java文件读写例子 java常用图片读写程序
爱学记

微信收款码
支付宝收款码