/** * @param dataSource * @throws XMLDBException */ private void storeSAXEvents(XMLReader dataSource) { try { if (root instanceof LocalCollection) { long t0 = System.currentTimeMillis(); LocalCollection coll = (LocalCollection) root; coll.setReader(dataSource); String existHome = System.getProperty("exist.home"); File existDir = existHome == null ? new File(".") : new File(existHome); doc.setContent(new File(existDir, FILE_STORED)); coll.storeResource(doc); long t1 = System.currentTimeMillis(); System.out.println("Time for storing: " + (t1 - t0) + " ms."); } } catch (XMLDBException e) { fail(e.getMessage()); } }
/** * Main method of the example class. * * @param args (ignored) command-line arguments * @throws Exception exception */ public static void main(final String[] args) throws Exception { System.out.println("=== XMLDBInsert ==="); // Collection instance Collection col = null; try { // Register the database Class<?> c = Class.forName(DRIVER); Database db = (Database) c.newInstance(); DatabaseManager.registerDatabase(db); System.out.println("\n* Get collection."); // Receive the collection col = DatabaseManager.getCollection(DBNAME); // ID for the new document String id = "world"; // Content of the new document String doc = "<xml>Hello World!</xml>"; System.out.println("\n* Create new resource."); // Create a new XML resource with the specified ID XMLResource res = (XMLResource) col.createResource(id, XMLResource.RESOURCE_TYPE); // Set the content of the XML resource as the document res.setContent(doc); System.out.println("\n* Store new resource."); // Store the resource into the database col.storeResource(res); } catch (final XMLDBException ex) { // Handle exceptions System.err.println("XML:DB Exception occurred " + ex.errorCode); ex.printStackTrace(); } finally { // Close the collection if (col != null) col.close(); } }