Ukázka XML:DB programu

Příklad 5. Příklad programu využívajícího XML:DB

import org.xmldb.api.base.*; 
import org.xmldb.api.modules.*; 
import org.xmldb.api.*; 
public class Query { 
    public static void main(String[] args) throws Exception { 
        Collection col = null; 
        try { 
            String driver = null; 
            String prefix = null; 

            if ( ( args.length == 1 ) && args[0].equals("dbxml") ) {
                driver = "org.dbxml.client.xmldb.DatabaseImpl"; 
                prefix = "xmldb:dbxml:///db/"; 

            } else { 
                driver = "org.xmldb.api.reference.DatabaseImpl"; 
                prefix = "xmldb:ref:///"; 
            } 

            Class c = Class.forName(driver); 
            Database database = (Database) c.newInstance(); 

            if ( ! database.getConformanceLevel().equals("1") ) { 
                System.out.println("This program requires a Core Level 1 XML:DB " + "API driver"); 
                System.exit(1); 
            }
            DatabaseManager.registerDatabase(database); 

            col = DatabaseManager.getCollection(prefix + "addresses"); 
            String xpath = "/address[@id = 1]"; 

            XPathQueryService service = (XPathQueryService) col.getService("XPathQueryService", "1.0"); 

            ResourceSet resultSet = service.query(xpath); 
            ResourceIterator results = resultSet.getIterator(); 

            while (results.hasMoreResources()) { 
                Resource res = results.nextResource(); 
                System.out.println((String) res.getContent()); 
            } 
        } catch (XMLDBException e) { 
            System.err.println("XML:DB Exception occurred " + e.errorCode + " " + e.getMessage()); 
        } finally { 
            if (col != null) { col.close(); } 
        } 
    }
}