我正在做一个学校项目,该项目需要7584行.csv行,并将其保存在带有eXist-db的.xml文件中。
这是我要插入的代码:
private static Collection collection = ExistConnection.getCollection();
private static XPathQueryService service;
static {
try {
service = (XPathQueryService) collection.getService("XPathQueryService", "1.0");
} catch (XMLDBException e) {
e.printStackTrace();
}
}
public static void insert() {
App.getCrimes().forEach(crime -> {
try {
String insert = "update insert" +
"<crime>" +
"<date>" + crime.getDate() + "</date>" +
"<address>" + crime.getAddress() + "</address>" +
"<district>" + crime.getDistrict() + "</district>" +
"<beat>" + crime.getBeat() + "</beat>" +
"<grid>" + crime.getGrid() + "</grid>" +
"<description>" + crime.getDescription() + "</description>" +
"<ncicCode>" + crime.getNcicCode() + "</ncicCode>" +
"<location>" +
"<latitude>" + crime.getLocation().getLatitude() + "</latitude>" +
"<longitude>" + crime.getLocation().getLongitude() + "</longitude>" +
"</location>" +
"</crime>" +
"into /crimes";
service.query(insert);
} catch (XMLDBException e) {
System.out.println(e.getMessage());
}
});
}
现在,用于计算犯罪数量的代码:
public static void countAll() {
try {
ResourceSet resourceSet = service.query("let $n := count(/crimes/crime) return $n");
ResourceIterator i = resourceSet.getIterator();
while (i.hasMoreResources()) {
Resource r = i.nextResource();
System.out.println((String) r.getContent());
}
ExistConnection.closeCollection();
} catch (XMLDBException e) {
e.printStackTrace();
}
}
当我运行该应用程序时,我得到了下一个错误以及计算犯罪数量的结果:
Failed to invoke method queryPT in class org.exist.xmlrpc.RpcConnection: org.exist.xquery.XPathException: err:XPST0003 unexpected token: $ (while expecting closing tag for element constructor: null) [at line 1, column 171]
Failed to invoke method queryPT in class org.exist.xmlrpc.RpcConnection: org.exist.xquery.XPathException: err:XPST0003 unexpected token: $ (while expecting closing tag for element constructor: null) [at line 1, column 177]
7582
我不知道为什么只能插入7584行中的7582行的原因。
我试着删除行7583和7584,以防万一由于某些原因这些行是错误的,但是错误仍然存在并且计数结果是7580。
最佳答案
.csv中发生错误,数据中两行的符号为<
,因此无法添加这两行。