« Return to Thread: XMLDBException on LocalXMLResource.getContentAsDOM()

Re: XMLDBException on LocalXMLResource.getContentAsDOM()

by Adam Retter-3 :: Rate this Message:

Reply to Author | View in Thread

> Thank you for spending the time to look into this. I am sorry that the
> sample was too complicated and messy. Next time I have an issue I will spend
> more time breaking it down to as little code as possible.

Thats ok, retrospectively I must appologise as I see now the tone of
my email was less than constructive in places!

However I think the lesson to take away here is that - simple, small
reproducible test cases will be addressed faster by us and receive
more attention as they really get to the crux of the problem.

For completeness of this, in your case, the reporducible example would of been -


public final static void main(String args[]) throws Exception {
        Database database = (Database)
Class.forName("org.exist.xmldb.DatabaseImpl").newInstance();
        database.setProperty("create-database", "true");
        DatabaseManager.registerDatabase(database);

        Collection rootCollection =
database.getCollection("exist:///db", "admin", "admin");
        Collection localCollection =
rootCollection.getChildCollection("preferences");

        XPathQueryService queryService = (XPathQueryService)
rootCollection.getService("XPathQueryService", "1.0");

        String query = "/systempreferences/preference[@group='group1']";

        ResourceSet results =
queryService.queryResource("systempreferences.xml", query);

        if(results.getSize() == 0) {
            // No results
            return;
        }

        // Parse out the results
        XMLResource resultBlob = (XMLResource) results.getMembersAsResource();

       // Display the results to console
       System.out.println(resultBlob.getContent());

       // get content as DOM for parsing
       // !!!!!!!!!! an XMLDBException is being thrown here !!!!!!!!!!
       Node node = resultBlob.getContentAsDOM();
       Document document = null;
       if (node.getOwnerDocument() == null) {
           document = (Document) node;
       }
       else {
           document = node.getOwnerDocument();
       }

       // create a list of all the preference nodes
       NodeList nodeList = document.getElementsByTagName("preference");

       // parse the list into a properties map
       Properties preferences = new Properties();
       for(int index = 0; index < nodeList.getLength(); index++) {
           // grab an element in the list
           Element element = (Element) nodeList.item(index);

           // add "value" to the properties map with the key "name"
           preferences.put(element.getAttribute("name"),
element.getAttribute("value"));

       }
    }



> The solution you have suggested offers a good work-around for the problem. I
> have also created a work-around solution myself which involved building the
> DOM Node by parsing the result content string. In this solution I basically
> replicated what RemoteXMLResource.getContentAsDOM() does.

I dont see it as a work around. I think it is the correct way to do it.
Your problem is really this line -

 resultBlob = (XMLResource) results.getMembersAsResource();


> I suppose at this point, it appears to me that my issue is due to how eXist
> implements the XML:DB API. I understand that it was probably not expected
> that a developer like myself parse the results of a query in this manner.

Are you aware of other xml db implementations that work in this way?

> But I am still not clear if what I am attempting to do is an illegal action
> according to the API spec.

To be honest I am not entirely sure myself, I do not use the xmldb
stuff myself. Some of the other eXist developers may be able to help
you here...

--
Adam Retter

eXist Developer
{ United Kingdom }
adam@...
irc://irc.freenode.net/existdb

------------------------------------------------------------------------------
_______________________________________________
Exist-open mailing list
Exist-open@...
https://lists.sourceforge.net/lists/listinfo/exist-open

 « Return to Thread: XMLDBException on LocalXMLResource.getContentAsDOM()