jsp读取xml文件。。。求讲解。。。新手啊
我想实现的是
从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>
从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>
...全文
相关推荐
Servlet读取XML文件实现JSP简单分页
Servlet读取XML文件实现JSP简单分页 博文链接:https://thtwin.iteye.com/blog/430521
JSP实现批量文件上传的详细教程
本文还有配套的精品资源,点击获取 简介:JSP(JavaServer Pages)是用于创建动态网页的技术,它允许开发者将Java代码嵌入到HTML页面中实现服务器端逻辑。本文将深入讲解JSP中实现批量文件上传的技术要点,包括创建HTML表单、处理Multipart请求、使用Apache Commons FileUpload库解析文件、文件存储策略、上传进度显示、错误处理和...
jsp 读取XML代码
jsp 读取XML代码,包括个文件,一个是JSP调用文件,一个是XML数据源结构文件,都在其中,注意XML数据源文件放的位置,经测试,可正常使用
SpringMVC 中xml文件的读取与显示
详细的介绍了SpringMVC 中xml文件的读取与显示,附带有jsp页面的实现
Servlet读取XML文件实现JSP分页
Servlet读取XML文件实现JSP分页,Servlet读取XML文件实现JSP分页


