0

I'm writing an application which allows for uploading a file to a specific collection in eXist-db. I am basing this application off code I found on the eXist web site.

Unfortunately, the code does not seem to work - when I test it an error message appears

usage: StoreExample collection-path document

When I change the URI xmldb:exist://localhost:8080/exist/xmlrpc to http://localhost:8080/exist/admin/admin.xql;jsessionid=1fkd05vvfv6kq and the collection to /db/col1, the following error occurs:

 Exception in thread "main" org.xmldb.api.base.XMLDBException: 
    at org.xmldb.api.DatabaseManager.getDatabase(Unknown Source)
    at org.xmldb.api.DatabaseManager.getCollection(Unknown Source)
    at org.xmldb.api.DatabaseManager.getCollection(Unknown Source)
    at addingfiletest.exp.main(exp.java:44)

ligne 44 ==> Collection col =DatabaseManager.getCollection(URI + collection);

Here is my code:

import java.io.File;
import org.xmldb.api.DatabaseManager;
import org.xmldb.api.base.Collection;
import org.xmldb.api.base.Database;
import org.xmldb.api.base.*;
import org.xmldb.api.modules.CollectionManagementService;
import org.xmldb.api.modules.XMLResource;

public class exp {

   public final static String URI = "http://localhost:8080/exist/admin/admin.xql;jsessionid=1fkd05vvfv6kq";

   public static void main(String args[]) throws Exception {

      String collection = "/db/col1", file = "D:/PFE/lien.txt";

      // initialisation du driver
      String driver = "org.exist.xmldb.DatabaseImpl";
      Class cl = Class.forName(driver);
      Database database = (Database) cl.newInstance();
      DatabaseManager.registerDatabase(database);

      // Accès à la collection
      Collection col = DatabaseManager.getCollection(URI + collection);

      // créer une nouvelle XMLResource; un id sera affecté à la nouvelle
      // ressource
      XMLResource document = (XMLResource) col.createResource(null,
            "XMLResource");
      File f = new File(file);
      if (!f.canRead()) {
         System.out.println("cannot read file " + file);
         return;
      }

      document.setContent(f);
      System.out.print("storing document " + document.getId() + "...");
      col.storeResource(document);
      System.out.println("ok.");
   }
}

All help appreciated,thanks.

Perception
  • 79,279
  • 19
  • 185
  • 195
sana
  • 29
  • 4

2 Answers2

1

I create a small application in the past and my solution for create new collections was: I used exists xqj libraries(ex - exists-xqj-1.0.1.jar; ex - xqj2-0.0.1.jar ; ex - xqjapi.jar first you need create the conexion:

private static void conectar() {
    try {
        xqc = crearconexion("localhost", "8080", "admin", "abc123.");
    } catch (XQException ex) {
        System.out.println("No hay conexion con la base de datos");
    }
}

    private static XQConnection crearconexion(String host, String puerto, String admin, String pass) throws XQException {
    xqjd = new ExistXQDataSource();
    xqjd.setProperty("serverName", host);
    xqjd.setProperty("port", puerto);
    xqjd.setProperty("user", admin);
    xqjd.setProperty("password", pass);
    xqc = xqjd.getConnection();
    return xqc;
}

(java) i have a method public to create this collection here (.....)

    consulta = 
             "declare namespace exist = \"http://exist.sourceforge.net/NS/exist\"; \n"
            + "declare namespace request=\"http://exist-db.org/xquery/request\"; \n"
            + "    declare namespace xmldb=\"http://exist-db.org/xquery/xmldb\";  \n"
            + "    declare variable $file as xs:string { \""
            + "<menu>\n"
            + "    <maquina>\n"
            + "        <listname>"+maquina.getNombre()+"</listname>\n"
            + "    </maquina>\n"
            + "    <game name=\'0\'>\n"
            + "        <description></description>\n"
            + "        <cloneof/>\n"
            + "        <manufacturer></manufacturer>\n"
            + "        <personalizado>\n"
            + "            <estrellas>0</estrellas>\n"
            + "            <lotengo>No</lotengo>\n"
            + "            <jugado>No</jugado>\n"
            + "            <rutaimagen/>\n"
            + "        </personalizado>\n"
            + "        <rating></rating>\n"
            + "        <year></year>\n"
            + "        <genre></genre>\n"
            + "        <clave>1</clave>\n"
            + "    </game>\n"
            + "</menu>\""
            + " };  \n"
            + "    declare variable $name as xs:string { \""+maquina.getFile()+"\" };  \n"
            + "    declare variable $collection as xs:string { \""+RUTAROOT+"\" };  \n"
            + "      \n"
            + "    <results>  \n"
            + "    {  \n"
            + "    let $load-status := xmldb:store($collection, $name, $file)  \n"
            + "    return <load-status> { $load-status } </load-status>  \n"
            + "    }  \n"
            + "    </results>  ";

        ejecutarConsultaU(consulta);

private static void ejecutarConsultaU(String cadenaAConsultar) throws XQException {
    XQExpression consulta;
    consulta = xqc.createExpression();
    System.out.println("Inicio consulta \n");
    System.out.println(cadenaAConsultar);//it shows your query
    System.out.println("fin consulta \n");
    consulta.executeCommand(cadenaAConsultar);
}
idelcano
  • 113
  • 7
0

To my knowledge, the folks who know the Java aspects of eXist-db are best reached via exist-open, the eXist-db mailing list, rather than here on stackoverflow. I'd suggest joining exist-open and posting your question there. You won't be disappointed. Also, remember to include the version # of eXist that you're running. Good luck!

Joe Wicentowski
  • 5,159
  • 16
  • 26