Java y eXist. Un par inseparable 2/2

Posted: marzo 14th, 2009 under eXist, java, XML, XQuery.

En el anterior post vimos como ejecutar una consulta en XQuery sobre eXist desde un programa escrito en Java. En este post continuamos viendo otros códigos  para realizar las operaciones más habituales sobre eXist desde Java.

En este primer ejemplo muestro el código fuente para recuperar un documento de la BD.


import org.xmldb.api.base.*;
import org.xmldb.api.modules.*;
import org.xmldb.api.*;
public class RetrieveExample {
public static void main(String args[]) throws Exception {
String driver = "org.exist.xmldb.DatabaseImpl"; //Driver
Class cl = Class.forName(driver);                        //Cargar Driver
Database database = (Database) cl.newInstance();  //Instancia de la BD
DatabaseManager.registerDatabase(database);       //Registrar DB
database.setProperty("create-database", "true");
Collection col = DatabaseManager.getCollection("xmldb:exist://localhost:8080/exist/xmlrpc/db/", "<<USER>>", "<<PASSWORD>>");
col.setProperty("pretty", "true");
col.setProperty("encoding", "ISO-8859-1");
XMLResource res = (XMLResource) col.getResource("prueba.xml");
if (res == null) {
System.err.println("could not retrieve document " + args[0] + "!");
return;
}
System.out.println((String) res.getContent()); //Recupero la inf.
}
}

En este muestro como añadir un documento XML a la BD.

import org.xmldb.api.*;
import org.xmldb.api.base.*;
import org.xmldb.api.modules.*;
import java.io.*;
import org.exist.util.*;
public class AddExample {
public static void main(String args[]) throws Exception {
String collection = args[0], file = args[1];
if (collection.startsWith("/db")) { // remove /db if specified
collection = collection.substring(3);
}
// initialize driver
String driver = "org.exist.xmldb.DatabaseImpl";
Class cl = Class.forName(driver);
Database database = (Database) cl.newInstance();
database.setProperty("create-database", "true");
DatabaseManager.registerDatabase(database);
// try to get collection
Collection col =DatabaseManager.getCollection("xmldb:exist://localhost:8080/exist/xmlrpc/db" + collection, "<<USER>>", "<<PASS>>");
if (col == null) {
// collection does not exist: get root collection and create it
Collection root = DatabaseManager.getCollection("xmldb:exist://localhost:8080/exist/xmlrpc/db", "<<user>>", "<<pass>>");
CollectionManagementService mgtService = (CollectionManagementService) root.getService("CollectionManagementService", "1.0");
col = mgtService.createCollection(collection);
}
// create new XMLResource; an id will be assigned to the new resource
XMLResource document = (XMLResource) col.createResource(null, "XMLResource");
File f = new File(file);
if (!f.canRead()) {
System.err.println("can't read file " + file);
}
document.setContent(f);
System.out.print("storing document " + document.getId() + "...");
col.storeResource(document);
System.out.println("ok.");
}
}

No hay comentarios »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment