jsp读取xml文件。。。求讲解。。。新手啊
心月缥缈 2014-12-10 10:06:58 我想实现的是
从xml读取出数据,在jsp中按照属性放置到tablelist中去。。。
现在运行 直接包错
重大: Servlet.service() for servlet [jsp] in context with path [/test2] threw exception [/trade_index.jsp (line: 46, column: 55) 等号記号が必要です] with root cause
org.apache.jasper.JasperException: /trade_index.jsp (line: 46, column: 55) 等号記号が必要です
在foreach里面 还有这些警告信息‘
Multiple annotations found at this line:
- Invalid location of text (()}") in
tag (<c:out>).
- Undefined attribute name
"getFirstChild"
- Undefined attribute name
"getNodeValue"
- Invalid location of text (").) in tag
(<c:out>).
- Invalid location of text ((0).) in
tag (<c:out>).
- Undefined attribute name "item"
- Undefined attribute name "title"
- Invalid location of text (().) in tag
(<c:out>).
jsp:
<body>
<%
//建立一个解析器工厂
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//获得一个具体的解析器对象
DocumentBuilder builder = factory.newDocumentBuilder();
//对XML文档进行解析,获得Document对象
Document doc = builder.parse("C:/workspace_fei/dummyData/books.xml");
doc.normalize();
//获取所有的book元素列表
NodeList books = doc.getElementsByTagName("book");
%>
<h2>图书列表</h2><br>
<%
for(int i=0; i<books.getLength(); i++){
//获取一个book元素
Element book = (Element) books.item(i);
%>
<table border="1" align="center">
<tr>
<td>title</td>
<td>URL</td>
<td>Author</td>
<td>Date</td>
<td>Description</td>
</tr>
<c:forEach var="cast" items="${book}" varStatus="st">
<tr>
<td><c:out value="${cast.getElementsByTagName("title").item(0).getFirstChild().getNodeValue()}" /></td>
<td><c:out value="${cast.getElementsByTagName("url").item(0).getFirstChild().getNodeValue()}" /></td>
<td><c:out value="${cast.getElementsByTagName("author").item(0).getFirstChild().getNodeValue() }" /></td>
<td><c:out value="${cast.pgetElementsByTagName("date").item(0)..getFirstChild().getNodeValue() }" />
</td>
<td><c:out value="${cast.getElementsByTagName("description").item(0).getFirstChild().getNodeValue() }" /></td>
</tr>
</c:forEach>
</table>
<%
}
%>
</body>
xml:
<?xml version="1.0" encoding="UTF-8"?>
<books>
<book>
<title>Book A</title>
<url newWindow="no">http://www.163.com</url>
<author>Author A</author>
<date>
<day>1</day>
<month>1</month>
<year>2009</year>
</date>
<description>This is a description for Book A</description>
</book>
<book>
<title>Book B</title>
<url newWindow="no">http://www.baidu.com</url>
<author>Author B</author>
<date>
<day>2</day>
<month>2</month>
<year>2009</year>
</date>
<description>This is a description for Book B</description>
</book>
<book>
<title>Book C</title>
<url newWindow="no">http://www.google.com</url>
<author>Author C</author>
<date>
<day>3</day>
<month>3</month>
<year>2009</year>
</date>
<description>This is a description for Book C</description>
</book>
</books>