jsp解析xml文档之使用 JDOM 读取XML文件
jsp教程解析xml文档之使用 JDOM 读取XML文件
xml文件:
<?xml version="1.0" encoding="GB2312"?>
<RESULT>
<VALUE>
<NO>A1234</NO>
<ADDR>四川省XX县XX镇XX路X段XX号</ADDR>
</VALUE>
<VALUE>
<NO>B1234</NO>
<ADDR>四川省XX市XX乡XX村XX组</ADDR>
</VALUE>
</RESULT>
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.zzzyk.com/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<body>
<%
JDOM
package test.xml;
import java.io.File;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
/**
* 使用 JDOM 读取XML文件。
*
*/
public class TestXMLByJDOM {
public static void main(String[] args) {
long lasting = System.currentTimeMillis();
System.out.println("Read By JDOM");
try {
// 构造
SAXBuilder builder = new SAXBuilder();
// 读取文档
Document doc = builder.build(new File("text.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();
}
System.out.println("运行时间:" + (System.currentTimeMillis() - lasting) + "毫秒");
}
}
%>
</body>
</html>
补充:Jsp教程,Java与XML