我想:
从URL读取xml数据并将其保存到名为agencies.xml的文件中
解析新创建的文件中的xml数据并显示数据。
我的尝试:
context.form.addButton
并且完整堆栈是:
<%@ page import="org.w3c.dom.*"%>
<%@ page import="javax.xml.parsers.*"%>
<%@ page import="java.io.File"%>
<%@ page import="java.io.IOException"%>
<%@ page import="java.net.URL"%>
<%@ page import="org.apache.commons.io.*"%>
<%
try {
URL url = new URL("http://10.12.123.12:1234/jspdemo/xmldata/xml_agency.jsp");
File destination = new File("agencies.xml");
// Copy bytes from the URL to the destination file.
FileUtils.copyURLToFile(url, destination);
} catch (IOException e) {
e.printStackTrace();
}
%>
<%
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse("agencies.xml");
NodeList agency = doc.getElementsByTagName("agency");
NodeList agencyId = doc.getElementsByTagName("agency_id");
NodeList agencyName = doc.getElementsByTagName("agency_name");
System.out.println("id:" + agencyId);
System.out.println("name:" + agencyName);
%>
<html>
<head>
<title>Read XML Data</title>
</head>
<body>
<table>
<form method="POST" action="ReadXML.jsp">
<tr>
<td>
<label>Agency ID</label>
</td>
<td>
<select name="filterName" style="text-align:center;">
<% for (int i=0; i<agency.getLength(); i++) { %>
<option value=><%= agencyId.item(i).getFirstChild().getNodeValue()%></option>
<% } %>
</select>
</td>
</tr>
<tr>
<td>
<label>Agency Name</label>
</td>
<td>
<select name="filterAge" style="text-align:center;">
<% for (int i=0; i<agency.getLength(); i++) { %>
<option value=><%= agencyName.item(i).getFirstChild().getNodeValue()%></option>
<% } %>
</select>
</td>
</tr>
<tr>
<td>
<a href="">Generate</a>
</td>
</tr>
</form>
</table>
</body>
</html>
错误发生在第1步。是否由于FileUtils类不能与JSP一起使用?因为它在.java类下工作。
我正在使用Eclipse和jdk1.8以及tomcat8。请建议我该怎么做才能解决错误。提前谢谢。