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