Home » Java » java – Error with eXist-db while I am trying to insert data-Exceptionshub

java – Error with eXist-db while I am trying to insert data-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

I’m doing a school project which takes 7584 rows of a .csv and save it in a .xml file with eXist-db.
Here is my code to insert:

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());
        }
    });
}

And now the code to count the number of crimes:

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();
    }
}

When I run the application I got the next error and the result of count the numbers of crimes:

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

I don’t know the reason why I only can insert 7582 of 7584 rows.
I tried to delete the rows 7583 and 7584, in case that these rows were wrong for some reason, but the error persists and the count result is 7580.

How to&Answers:

There was an error in the .csv, 2 rows had the symbol < inside the data and these 2 rows couldn’t be added.