From: Guy De S. <mai...@de...> - 2002-12-03 07:57:32
|
----- Original Message ----- From: "Guy De Schepper" <mai...@de...> To: <exi...@li...> Sent: Monday, December 02, 2002 21:12 Subject: [Exist-open] cannot import result in existing document > I have problems importing the result of a query in an xml Document object. > > I'm using the xmldb api, and try to do the following: > > > // Query the database: > > public synchronized Document queryDocument(String p_collection, String > p_xpath) { > Collection col = null; > XPathQueryService service = null; > Document doc = null; > > try { > if (EXIST) { > // for eXist, add an extra * at the end of the query > p_xpath += "*"; > } > col = getCollection(p_collection); > System.out.println("col = " + col); > service = (XPathQueryService) col.getService("XPathQueryService", > XMLDBAPIVERSION); > System.out.println("p_xpath = " + p_xpath); > ResourceSet resultSet = service.query(p_xpath); > doc = combineXPathResults(resultSet); > } > catch (XMLDBException exc) { > System.err.println("error requesting data from database"); > exc.printStackTrace(); > } > > return doc; > } > > > // Everything still works fine > > // combine all results in one document > public Document combineXPathResults(ResourceSet result) throws > XMLDBException { > // Create a new Document and a root element that will > // have all the results as its children > Document resultDoc = null; > Element root = null; > > resultDoc = new DocumentImpl(); > > // Iterate the xpath results and add each of them to the main Document > ResourceIterator iterator = result.getIterator(); > if (iterator.hasMoreResources()) { > root = resultDoc.createElement(XMLRESULT_TOP_ELEMENT); > resultDoc.appendChild(root); > System.out.println("docelem = " + > resultDoc.getDocumentElement().getNodeName()); // this displays: docelem = > result > while(iterator.hasMoreResources()) { > Resource r = iterator.nextResource(); > Element resElement; > Node mynode = null; > if (EXIST) { > System.out.println("resource = " + r); // output: resource = > org.exist.xmldb.LocalXMLResource@4bfe9d > System.out.println("resourcetype = " + r.getResourceType()); // > output: resourcetype = XMLResource > mynode = ((XMLResource)r).getContentAsDOM(); > System.out.println("mynode = " + mynode.getNodeName()); // > output: mynode = configuration > resElement = (Element) mynode; > System.out.println("resElement = " + resElement.getNodeName()); > // output: resElement = configuration > } > else { > resElement = ((Document) > ((XMLResource)r).getContentAsDOM()).getDocumentElement(); > } > > // Remove unwanted attributes (for XIndice) > resElement.removeAttribute("src:col"); > resElement.removeAttribute("src:key"); > resElement.removeAttribute("xmlns:src"); > > System.out.println("resElement = " + resElement.getNodeName()); // > output: resElement = configuration > System.out.println("docelem = " + > resultDoc.getDocumentElement().getNodeName()); // output: docelem = result > // Add this result to the root element > Element importedElement = (Element)resultDoc.importNode(resElement, > true); > > // here I get an exception: > Exception in thread "main" java.lang.NullPointerException > at > org.apache.xerces.dom.CoreDocumentImpl.importNode(CoreDocumentImpl.java:899) > at > org.apache.xerces.dom.CoreDocumentImpl.importNode(CoreDocumentImpl.java:862) > at > be.securit.dman.XmlDbAdapter.combineXPathResults(XmlDbAdapter.java:252) > at be.securit.dman.XmlDbAdapter.queryDocument(XmlDbAdapter.java:200) > at be.securit.dman.DataManager.populate(DataManager.java:254) > at be.securit.dman.DataManager.init(DataManager.java:226) > at be.securit.dman.DataManager.main(DataManager.java:215) > // > > root.appendChild(importedElement); > } > > > } > > return resultDoc; > } > > > When I run the same code with XIndice, I don't have this problem. > > > Any ideas what could be wrong ? > > > /* Guy */ > > > > ------------------------------------------------------- > This SF.net email is sponsored by: Get the new Palm Tungsten T > handheld. Power & Color in a compact size! > http://ads.sourceforge.net/cgi-bin/redirect.pl?palm0002en > _______________________________________________ > Exist-open mailing list > Exi...@li... > https://lists.sourceforge.net/lists/listinfo/exist-open > When I try to cast the result to a Document element (as I do with XIndice): Document mydoc = (Document) ((XMLResource)r).getContentAsDOM(); I get following exception: Exception in thread "main" java.lang.ClassCastException: org.exist.dom.ElementImpl at be.securit.dman.XmlDbAdapter.combineXPathResults(XmlDbAdapter.java:239) at be.securit.dman.XmlDbAdapter.queryDocument(XmlDbAdapter.java:200) at be.securit.dman.DataManager.populate(DataManager.java:254) at be.securit.dman.DataManager.init(DataManager.java:226) at be.securit.dman.DataManager.main(DataManager.java:215) This indicates exist uses its own ElementImpl Object. How can I convert it to an org.w3c.dom.ElementImpl Object ? Or maybe there are other possibilities to import this Node into an existing Document (org.w3c.dom.DocumentImpl) Object ? /* Guy */ |