我的代码就像:
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet;
import org.dom4j;
import org.jaxen.JaxenException;
/**
* Servlet implementation class Search
*/
//@WebServlet("/Search")
public class Search extends HttpServlet {
private static final long serialVersionUID = 1L;
private static String xmlName = "rows.xml";
private static Document document;
/**
* @throws DocumentException
* @see HttpServlet#HttpServlet()
*/
public Search() throws DocumentException {
super();
XmlReader xmlReader = new XmlReader();
document = xmlReader.readXml();
searcher();
}
发生意外输出的代码:
public void searcher()
{
List nodelist = document.selectNodes("rows");
System.out.println(nodelist.size());
}
}
我的XML阅读器:
package web.app;
import org.xml.sax.helpers.DefaultHandler;
import org.dom4j;
public class XmlReader extends DefaultHandler{
public static String filename =
"D:\\JavaWorkplace\\DataCuration\\WebContent\\rows.xml";
public Document readXml() throws DocumentException {
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(filename);
System.out.println(document.content());
return document;
}
}
XML文件:https://data.oregon.gov/api/views/j8eb-8um2/rows.xml?accessType=DOWNLOAD 但是,输出是:
[org.dom4j.tree.DefaultElement@50e96ea1 [Element: <response attributes: []/>]]
0
那该怎么了?
答案 0 :(得分:0)
好吧,我明白了。 我混淆了XML文件中的根元素。 它是&#34;响应&#34;而不是&#34;行&#34;。 无论如何,谢谢:)