1.XML文件
<?
xml version="1.0" encoding="GB2312"
?>
<!--
个人履历表
-->
<
resume
>
<
person
id
="01"
>
<
name
>
张三
</
name
>
<
birthday
>
03/24/1975
</
birthday
>
<
phone
>
1111-1111
</
phone
>
<
address
>
大连
</
address
>
</
person
>
<
person
id
="02"
>
<
name
>
李四
</
name
>
<
birthday
>
9/26/1978
</
birthday
>
<
phone
>
2222-2222
</
phone
>
<
address
>
南京
</
address
>
</
person
>
<
person
id
="03"
>
<
name
>
王五
</
name
>
<
birthday
>
11/09/1979
</
birthday
>
<
phone
>
3333-3333
</
phone
>
<
address
>
武汉
</
address
>
</
person
>
<
person
id
="04"
>
<
name
>
赵六
</
name
>
<
birthday
>
6/04/1973
</
birthday
>
<
phone
>
4444-4444
</
phone
>
<
address
>
青岛
</
address
>
</
person
>
<
person
id
="05"
>
<
name
>
陈七
</
name
>
<
birthday
>
12/19/1977
</
birthday
>
<
phone
>
5555-5555
</
phone
>
<
address
>
上海
</
address
>
</
person
>
</
resume
>
2.MyDOMBean.java
package
test;

import
org.xml.sax.
*
;
import
javax.xml.parsers.
*
;
import
org.w3c.dom.
*
;
import
java.io.
*
;


public
class
MyDOMBean
implements
java.io.Serializable
...
{
private static String xmlStr="";
private static final String PATH="file:///";


public MyDOMBean() ...{
}


public String getString()...{
return xmlStr;
}


public static Document getDocument(String filename) throws Exception ...{
xmlStr="";
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
// 设定解析的叁数
dbf.setIgnoringComments(true);
dbf.setIgnoringElementContentWhitespace(true);
DocumentBuilder db = dbf.newDocumentBuilder();
//导入XML文件
Document doc = db.parse(PATH+filename);
return doc;
}


public void traverseTree(Node node) throws Exception ...{

if(node == null) ...{
return;
}
int type = node.getNodeType();


switch (type) ...{
// handle document nodes

case Node.DOCUMENT_NODE: ...{
xmlStr+="<tr>";
traverseTree(((Document)node).getDocumentElement());
break;
}
// handle element nodes

case Node.ELEMENT_NODE: ...{
String elementName = node.getNodeName();

if(elementName.equals("person")) ...{
xmlStr+="</tr><tr>";
}
NodeList childNodes =
node.getChildNodes();

if(childNodes != null) ...{
int length = childNodes.getLength();
for (int loopIndex = 0; loopIndex <
length ; loopIndex++)

...{
traverseTree(childNodes.item(loopIndex));
}
}
break;
}
// handle text nodes

case Node.TEXT_NODE: ...{
String data = node.getNodeValue().trim();

if((data.indexOf(" ") <0) && (data.length()> 0)) ...{
xmlStr+="<td>"+data+"</td>";
}
}
}
}

}
3.jsp文件
<
html
>
<
head
>
<
title
>
使用DOM解析器
</
title
>
</
head
>

<%
...
@ page import="org.w3c.dom.*"
%>

<%
...
@ page contentType="text/html;charset=GB2312"
%>
<
body
bgcolor
="#CFF1E1"
>
<
center
>
<
h2
>
个人履历表(DOM版)
</
h2
>
<
table
border
="1"
width
="80%"
>
<
tr
>
<
td
>
姓名
</
td
>
<
td
>
出生年月
</
td
>
<
td
>
电话号码
</
td
>
<
td
>
居住地
</
td
>
</
tr
>
<
jsp:useBean
id
="domparser"
class
="test.MyDOMBean"
/>

<%
...
Document doc = domparser.getDocument(request.getRealPath("/") + "08_02.xml");
domparser.traverseTree(doc);
out.print(domparser.getString());
%>
</
body
>
</
html
>
<?
xml version="1.0" encoding="GB2312"
?>
<!--
个人履历表
-->
<
resume
>
<
person
id
="01"
>
<
name
>
张三
</
name
>
<
birthday
>
03/24/1975
</
birthday
>
<
phone
>
1111-1111
</
phone
>
<
address
>
大连
</
address
>
</
person
>
<
person
id
="02"
>
<
name
>
李四
</
name
>
<
birthday
>
9/26/1978
</
birthday
>
<
phone
>
2222-2222
</
phone
>
<
address
>
南京
</
address
>
</
person
>
<
person
id
="03"
>
<
name
>
王五
</
name
>
<
birthday
>
11/09/1979
</
birthday
>
<
phone
>
3333-3333
</
phone
>
<
address
>
武汉
</
address
>
</
person
>
<
person
id
="04"
>
<
name
>
赵六
</
name
>
<
birthday
>
6/04/1973
</
birthday
>
<
phone
>
4444-4444
</
phone
>
<
address
>
青岛
</
address
>
</
person
>
<
person
id
="05"
>
<
name
>
陈七
</
name
>
<
birthday
>
12/19/1977
</
birthday
>
<
phone
>
5555-5555
</
phone
>
<
address
>
上海
</
address
>
</
person
>
</
resume
>
2.MyDOMBean.java
package
test;
import
org.xml.sax.
*
;
import
javax.xml.parsers.
*
;
import
org.w3c.dom.
*
;
import
java.io.
*
;

public
class
MyDOMBean
implements
java.io.Serializable
...
{
private static String xmlStr="";
private static final String PATH="file:///";

public MyDOMBean() ...{
}

public String getString()...{
return xmlStr;
}

public static Document getDocument(String filename) throws Exception ...{
xmlStr="";
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
// 设定解析的叁数
dbf.setIgnoringComments(true);
dbf.setIgnoringElementContentWhitespace(true);
DocumentBuilder db = dbf.newDocumentBuilder();
//导入XML文件
Document doc = db.parse(PATH+filename);
return doc;
}

public void traverseTree(Node node) throws Exception ...{
if(node == null) ...{
return;
}
int type = node.getNodeType();

switch (type) ...{
// handle document nodes
case Node.DOCUMENT_NODE: ...{
xmlStr+="<tr>";
traverseTree(((Document)node).getDocumentElement());
break;
}
// handle element nodes
case Node.ELEMENT_NODE: ...{
String elementName = node.getNodeName();
if(elementName.equals("person")) ...{
xmlStr+="</tr><tr>";
}
NodeList childNodes =
node.getChildNodes();
if(childNodes != null) ...{
int length = childNodes.getLength();
for (int loopIndex = 0; loopIndex <
length ; loopIndex++)
...{
traverseTree(childNodes.item(loopIndex));
}
}
break;
}
// handle text nodes
case Node.TEXT_NODE: ...{
String data = node.getNodeValue().trim();
if((data.indexOf(" ") <0) && (data.length()> 0)) ...{
xmlStr+="<td>"+data+"</td>";
}
}
}
}
}
3.jsp文件
<
html
>
<
head
>
<
title
>
使用DOM解析器
</
title
>
</
head
>

<%
...
@ page import="org.w3c.dom.*"
%>

<%
...
@ page contentType="text/html;charset=GB2312"
%>
<
body
bgcolor
="#CFF1E1"
>
<
center
>
<
h2
>
个人履历表(DOM版)
</
h2
>
<
table
border
="1"
width
="80%"
>
<
tr
>
<
td
>
姓名
</
td
>
<
td
>
出生年月
</
td
>
<
td
>
电话号码
</
td
>
<
td
>
居住地
</
td
>
</
tr
>
<
jsp:useBean
id
="domparser"
class
="test.MyDOMBean"
/>

<%
...
Document doc = domparser.getDocument(request.getRealPath("/") + "08_02.xml");
domparser.traverseTree(doc);
out.print(domparser.getString());
%>
</
body
>
</
html
>
本文介绍了一个使用DOM解析XML文件的具体示例,展示了如何通过Java实现XML文件的读取与解析,并将结果展示在JSP页面上。代码示例中包含了XML文件结构、Java解析类MyDOMBean以及JSP页面展示部分。
3668

被折叠的 条评论
为什么被折叠?



