jsp中读取xml文件中内容的方法,案例如下:
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 testimport 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) ...{
// *** 作DOCUMENT对象节点
case Node.DOCUMENT_NODE: ...{
xmlStr+="<tr>"
traverseTree(((Document)node).getDocumentElement())
break
}
// *** 作XML元素节点
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
}
// *** 作XML文本节点
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/htmlcharset=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>
额,本人才疏学浅,这两者之间从编程语言上来说基本上没啥子关系,不过你要用jsp做动态网页可以把xml作为数据库。当然如果用到hibernate或者spring之类的技术,是要用xml文件写配置的。比如用hibernate+jsp+MySQL的话,你需要写一个hibernate的xml配置文件用来制定连接的数据库,写类的时候要用xml写一个类和数据库中表的映射文件。
这么看来xml与jsp还是有不少联系的,我知道的就这些,希望能够帮到你
JSP将HTML的表单数据提交生成一个XML文件的方法:1、定义表单,封装id,firstName和lastName
<form:form modelAttribute="person">
<form:hidden path="id" />
<fieldset>
<div class="form-row">
<label for="firstName"><fmt:message key="person.form.firstName"/>:</label>
<span class="input"><form:input path="firstName" /></span>
</div>
<div class="form-row">
<label for="lastName"><fmt:message key="person.form.lastName"/>:</label>
<span class="input"><form:input path="lastName" /></span>
</div>
<div class="form-buttons">
<div class="button">
<input type="submit" id="save" name="_eventId_save" value="<fmt:message key="button.save"/>"/>
<input type="submit" name="_eventId_cancel" value="Cancel"/>
</div>
</div>
</fieldset>
</form:form>
2、提交到servlet后,调用构造xml的java方法:
try {
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance()
DocumentBuilder docBuilder = docFactory.newDocumentBuilder()
// root elements
Document doc = docBuilder.newDocument()
Element rootElement = doc.createElement("company")
doc.appendChild(rootElement)
// staff elements
Element staff = doc.createElement("Staff")
rootElement.appendChild(staff)
// set attribute to staff element
Attr attr = doc.createAttribute("id")
attr.setValue("1")
staff.setAttributeNode(attr)
// shorten way
// staff.setAttribute("id", "1")
// firstname elements
Element firstname = doc.createElement("firstname")
firstname.appendChild(doc.createTextNode("yong"))
staff.appendChild(firstname)
// lastname elements
Element lastname = doc.createElement("lastname")
lastname.appendChild(doc.createTextNode("mook kim"))
staff.appendChild(lastname)
// nickname elements
Element nickname = doc.createElement("nickname")
nickname.appendChild(doc.createTextNode("mkyong"))
staff.appendChild(nickname)
// salary elements
Element salary = doc.createElement("salary")
salary.appendChild(doc.createTextNode("100000"))
staff.appendChild(salary)
// write the content into xml file
TransformerFactory transformerFactory = TransformerFactory.newInstance()
Transformer transformer = transformerFactory.newTransformer()
DOMSource source = new DOMSource(doc)
StreamResult result = new StreamResult(new File("d:\\person.xml"));
transformer.transform(source, result)
System.out.println("File saved!")
} catch (ParserConfigurationException pce) {
pce.printStackTrace()
} catch (TransformerException tfe) {
tfe.printStackTrace()
}
3、构造完成:
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<company>
<staff id="1">
<firstname>yong</firstname>
<lastname>mook kim</lastname>
<nickname>mkyong</nickname>
<salary>100000</salary>
</staff>
</company>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)