使用XPath解析时,得到了一个零大小列表XML
问题描述:
我的代码是这样的:使用XPath解析时,得到了一个零大小列表XML
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 但是,输出是:
[[email protected] [Element: <response attributes: []/>]]
0
那么,有什么不好呢?
答
好的,我找到了原因。 我混淆了XML文件中的根元素。 它是“响应”,而不是“行”。 无论如何感谢:)
您使用哪种XML框架?它是dom4j吗?请澄清并提供足够的背景下,其他人可以测试您的代码。即发布[mcve] –
是的,我正在使用dom4j。 – Chen
没有产生错误的[mcve],任何人都很难帮助你。事实上,在准备MCVE时,你很可能会自己找出问题所在。 –