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>
...全文
449 5 打赏 收藏 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
心月缥缈 2014-12-19
  • 打赏
  • 举报
回复
擦擦擦 没人回复下 我分都给不出去啊。。。 救命啊 啊啊啊啊啊 来人就送分了
心月缥缈 2014-12-19
  • 打赏
  • 举报
回复
好了 分送出去了 结帖
Sean_214 2014-12-19
  • 打赏
  • 举报
回复
你这个“<c:forEach var="cast" items="${book}" varStatus="st">”标签写的有问题,前边已经有for循环了,book本身就是单个的<book>节点元素了,不用再循环了。
阿良良木月火 2014-12-19
  • 打赏
  • 举报
回复
不是专业,来蹭个分
心月缥缈 2014-12-11
  • 打赏
  • 举报
回复
用下面的方法解决了。。。 有没有大神能提供更简单的方法。。 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { SAXReader sr = new SAXReader();// 获取读取方式 Document doc; String title = ""; try { // 写死的地址,需要改,放在配置文件里面或者servlet属性里 String filePath = "C:\\workspace_fei\\test\\WebContent\\trade_info.xml"; File file = new File(filePath); if (file.exists()) { doc = sr.read(file); // 读取xml文件,并且将数据全部存放到Document中 Element root = doc.getRootElement();// 获取根节点 TradeInfo commodity = null; Element commodityele = null;// book节点 List<TradeInfo> commoditys = new ArrayList<TradeInfo>(); for (Iterator i = root.elementIterator("commodity"); i .hasNext();) {// 从book节点迭代 commodityele = (Element) i.next(); commodity = new TradeInfo(); title = commodityele.element("title").getText(); if(!title.equals("end")){ commodity.setTitle(commodityele.element("title").getText()); commodity.setBuyer_nick(commodityele.element("buyer_nick") .getText());

51,407

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧