xml文件
Xml代碼
<?xml version=
<RESULT>
<VALUE>
<NO>A
<ADDR>河南省鄭州市</ADDR>
</VALUE>
<VALUE>
<NO>B
<ADDR>河南省鄭州市二七區</ADDR>
</VALUE>
</RESULT>
第一種 DOM 實現方法
Java代碼
import java
import javax
import javax
import org
import org
public class MyXMLReader
public static void main(String arge[]) {
long lasting = System
try {
File f = new File(
DocumentBuilderFactory factory = DocumentBuilderFactory
DocumentBuilder builder = factory
Document doc = builder
NodeList nl = doc
for (int i =
System
System
System
+
}
}
} catch (Exception e) {
e
}
}
}
第二種
Java代碼
import java
import java
import org
import org
public class MyXMLReader
public static void main(String arge[]) {
long lasting = System
try {
File f = new File(
SAXReader reader = new SAXReader();
Document doc = reader
Element root = doc
Element foo;
for (Iterator i = root
foo = (Element) i
System
System
}
System
+
}
} catch (Exception e) {
e
}
}
}
第三種 JDOM實現方法
Java代碼
import java
import java
import org
import org
public class MyXMLReader
public static void main(String arge[]) {
long lasting = System
try {
SAXBuilder builder = new SAXBuilder();
Document doc = builder
Element foo = doc
List allChildren = foo
for (int i =
System
System
}
System
+
}
} catch (Exception e) {
e
}
}
}
第四種SAX實現方法
Java代碼
import javax
import javax
import org
import org
import org
import org
public class MyXMLReader
java
public MyXMLReader
super();
}
public static void main(String args[]) {
long lasting = System
try {
SAXParserFactory sf = SAXParserFactory
SAXParser sp = sf
MyXMLReader
sp
} catch (Exception e) {
e
}
System
+
}
public void characters(char ch[]
throws SAXException {
String tag = (String) tags
if (tag
System
}
if (tag
System
}
}
public void startElement(String uri
Attributes attrs) {
tags
}
}
From:http://tw.wingwit.com/Article/program/Java/hx/201311/25610.html