例:如下xml(你可以自己添加或删除一些节点进行测试)
<?xml version="1.0" encoding="gbk"?>
<root>
<pnode>
<province>
<pid>10056</pid>
<pname>山东</pname>
<cnode>
<city>
<cid>11017</cid>
<cname>潍坊</cname>
<znode>
<zone>
<zid>11018</zid>
<zname>农垦</zname>
<nnode>
<node>
<nid>11019</nid>
<nname>试验点</nname>
</node>
<node>
<nid>10020</nid>
<nname>测试</nname>
</node>
</nnode>
<zid>11018</zid>
<zname>农垦a </zname>
<nnode>
<node>
<nid>1101a 9</nid>
<nname>试验点a </nname>
</node>
<node>
<nid>1102a 0</nid>
<nname>测试a </nname>
</node>
</nnode>
</zone>
</znode>
</city>
<city>
<cid>11028</cid>
<cname>诸城</cname>
<znode>
<zone>
<zid>11029</zid>
<zname>诸城都是烟田</zname>
<nnode>
<node>
<nid>11030</nid>
<nname>不信你去看</nname>
</node>
<node>
<nid>11041</nid>
<nname>你去看啊</nname>
</node>
</nnode>
</zone>
</znode>
</city>
</cnode>
</province>
</pnode>
</root>
解析方法:
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
/**
* 这里用一句话描述这个类的作用.
*
* @author 杜文俊
*/
public class newXMLParse {
Document document = null;
NodeList nodeList = null;
/**
* 构造函数,初始化Document对象 类的构造方法.
*/
public newXMLParse() {
// 指定File文件
File file = new File("E:\\test.xml");
// 建立DocumentBuilderFactory对象
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
try {
// 建立DocumentBuilder对象
builder = builderFactory.newDocumentBuilder();
// 用DocumentBuilder对象的parse方法引入文件建立Document对象
document = builder.parse(file);
nodeList = document.getChildNodes();
// province 根据你的xml标签进行解析
NodeList province = document.getElementsByTagName("province");
// 测试1:找出所有province标签,返回NodeList
showByCondition(province);
// 测试2:遍历所有节点
//searchAndShow(nodeList);
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
System.err.println("找不到你指定的文件!");
e.printStackTrace();
}
}
// 测试 1 方法:按条件输出
public void showByCondition(NodeList nodeList) {
System.out.println();
System.out.println("------showByCondition输出结果-------");
Element element;
// 对符合条件的所有节点进行遍历
int nodeListLength = nodeList.getLength();
for (int i = 0; i < nodeList.getLength(); i++) {
// 获得一个元素
element = (Element) nodeList.item(i);
// 此元素有子节点,获取所有子节点,返回一个provinceList
NodeList provinceList = element.getChildNodes();
int provinceListLength = provinceList.getLength();
// 遍历所有子节点
for (int j = 0; j < provinceList.getLength(); j++) {
// 若子节点的名称不为#text,则输出,#text为反/标签
// provinceList.item(j).getNodeName()获取标签的名字
String nodenameStr = provinceList.item(j).getNodeName();
if (!provinceList.item(j).getNodeName().equals("#text")) {
// 输出节点名称、节点值
System.out.println(provinceList.item(j).getNodeName() + ":"+ provinceList.item(j).getTextContent());
}
}
}
}
// 测试2 方法:寻找遍历
public void searchAndShow(NodeList nodeList) {
System.out.println();
System.out.println("--------- 接下来让我们看一下解析XML后得到的数据 ---------");
for (int i = 0; i < nodeList.getLength(); i++) {
// 得到一个节点,需要强制转换为Element,这里Node是Element的父类
Node node = nodeList.item(i);
// if (!node.getNodeName().equals("#text")) {
// System.out.println(node.getNodeName());
// }
// System.out.println(element.getAttribute(""));
int childNodesLength = node.getChildNodes().getLength();
if (node.getChildNodes().getLength() > 3) {
// 若包含子节点,则递归遍历
System.out.println("此节点包含子元素!");
searchAndShow(node.getChildNodes());
System.out.println();
} else {
// 若不包含子节点,则输出节点中的内容
if (!node.getTextContent().trim().equals("") && node.getTextContent() != null) {
System.out.println(node.getTextContent());
}
}
}
}
/**
* 程序入口
*
* @param args
* @author
* @update 2013-6-3 下午7:29:17
*/
public static void main(String[] args) {
new newXMLParse();
}
}