I modified the code this :

XMLResource res = (XMLResource)results.nextResource();
newNode = newDoc.importNode(((Document)
  res.getContentAsDOM()).getDocumentElement(), true);
root.appendChild(newNode);

Yet a I still get a org.w3c.dom.DOMException: DOM003
Namespace error. I had to cast res.getContentAsDOM()
to a Document to use the getDocumentElement method.
Was this correct?

--- Jeff Greif <[EMAIL PROTECTED]> wrote:
> A Document node may not be the child of any other
> node, I think.  You need
> to add the root element of the document to the new
> document, something like
> this:
> 
>     newNode =
>        
>
newDoc.importNode(res.getContentAsDOM().getDocumentElement(),
>                                          true);
> 
> Jeff
> 
> ----- Original Message -----
> From: "Evan Borysko" <[EMAIL PROTECTED]>
> To: <[email protected]>
> Sent: Tuesday, November 12, 2002 12:07 PM
> Subject: Re: Problem with adding DOM node from
> getContentAsDOM() to other
> DOM
> 
> 
> 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.
> 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