It seems that even this solution throws and error when you attempt importNode. It seems the type returned by getContentAsDOM method is org.apache.xindice.xml.dom.DocumentImpl. The JavaDoc claims the should return type org.w3c.dom.Node.

The DocumentImpl is a type Node, but does seem that it can be inserted into another DOM document.

I work up two examples. The first that demonstrates how it looks like it should work, and second is my quick fix fix using DOM4J instead of the standard w3c classes. By no means am I an expert on theses API's, and I would appreciate any suggestions to improve them.

The second example works, and outs the code wraps the results in a <Results> root tag.

import org.xmldb.api.base.*;
import org.xmldb.api.modules.*;
import org.dom4j.*;
import org.dom4j.io.DOMReader;
import org.xmldb.api.*;

public class Example1 {
   public static void main(String[] args) throws Exception {
      Collection col = null;
      try {
         String driver = "org.apache.xindice.client.xmldb.DatabaseImpl";
         Class c = Class.forName(driver);

         Database database = (Database) c.newInstance();
         DatabaseManager.registerDatabase(database);

col = DatabaseManager.getCollection("xmldb:xindice:///db/Classifieds");
String xpath = "//[EMAIL PROTECTED]'105' or @id='100' or @id='500']";


         Document doc, newdoc;
         DocumentFactory df = DocumentFactory.getInstance();
         newdoc = df.createDocument(df.createElement("Results"));

XPathQueryService service =
(XPathQueryService) col.getService("XPathQueryService", "1.0");
ResourceSet resultSet = service.query(xpath);
ResourceIterator results = resultSet.getIterator();
while (results.hasMoreResources()) {
XMLResource res = (XMLResource)results.nextResource();
DOMReader xmlReader = new DOMReader();
doc = xmlReader.read(((org.w3c.dom.Document) res.getContentAsDOM()));
Element elm = doc.getRootElement();
newdoc.getRootElement().add(elm);
System.out.println(doc.getRootElement().getName());


         }
        System.out.println(newdoc.asXML());
      }
      catch (XMLDBException e) {
         System.err.println("XML:DB Exception occured " + e.errorCode);
      }
      finally {
         if (col != null) {
            col.close();
         }
      }
   }
}

____ And the Second _____

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.xmldb.api.base.*;
import org.xmldb.api.modules.*;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.xmldb.api.*;

public class Example2 {
   public static void main(String[] args) throws Exception {
      Collection col = null;
      try {
         String driver = "org.apache.xindice.client.xmldb.DatabaseImpl";
         Class c = Class.forName(driver);

         Database database = (Database) c.newInstance();
         DatabaseManager.registerDatabase(database);

col = DatabaseManager.getCollection("xmldb:xindice:///db/Classifieds");

String xpath = "//[EMAIL PROTECTED]'105' or @id='100' or @id='500']";

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document newDoc = builder.newDocument();
//Document doc;
Element root = newDoc.createElement("Results");
newDoc.appendChild(root);


XPathQueryService service =
(XPathQueryService) col.getService("XPathQueryService", "1.0");
ResourceSet resultSet = service.query(xpath);
ResourceIterator results = resultSet.getIterator();


         Node newNode;
         while (results.hasMoreResources()) {
            XMLResource res = (XMLResource)results.nextResource();

            newNode = newDoc.importNode(res.getContentAsDOM(), true);
            root.appendChild(newNode);
                        
         }
        System.out.println(newDoc.toString());
      }
      catch (XMLDBException e) {
         System.err.println("XML:DB Exception occured " + e.errorCode);
      }
      finally {
         if (col != null) {
            col.close();
         }
      }
   }
}

Please let me know if their is a better way to handle these issues.

Cheers,
Evan

--------------------------------------
Evan Borysko
[EMAIL PROTECTED]

"The programmer, like the poet, works only slightly removed from pure thought-stuff. He builds his castles in the air, from air, creating by exertion of the imagination."
--Frederick Brooks, Jr.


On Monday, November 11, 2002, at 09:25 AM, Jeff Greif wrote:

Yes this is how the DOM is specified to work. Normally you save the extra
code by using
newNode = destinatinationDocument.importNode(Node sourcceNode, true);
someDestinationNode.appendChild(newNode);
The boolean flag on importNode indicates whether to clone the entire subtree
specified by the sourceNode, or just the root.


There are a few subtleties, so looking at the javadocs for importNode is a
good idea.


Jeff
----- Original Message -----
From: "�yvind Vestavik" <[EMAIL PROTECTED]>
To: <[email protected]>
Sent: Monday, November 11, 2002 2:27 AM
Subject: Re: Problem with adding DOM node from getContentAsDOM() to other
DOM




It seems that I was only allowed to add to a
DOM document nodes that were created using the same document, ie
doc.createElement(myElement) where doc is the document to wich I want to
add myElement.






Reply via email to