Hi Nani,

I think this is an XML problem, not really a Xindice one. If you check the 
class of the node returned by getContentAsDOM, you'll find it is a 
DocumentImpl. 

                Node contentNode = xmlResource.getContentAsDOM();
            System.out.println("contentNode = " + 
contentNode.getClass().getName());

As you know, a well formed document can't have more than one root element, so 
if you try to add another root child, it is no longer well formed. This is 
presumably what Xindice is complaining about.

If you add the newElement node to the first child of the node, instead of to 
the node itself, there is no problem.

                Element newElement = doc.createElement("newchild");
            contentNode.getFirstChild().appendChild(newElement);
            xmlResource.setContentAsDOM(contentNode);
            collection.storeResource(xmlResource);

This begs the question, of course, of whether it makes sense to enforce the 
original structure of stored documents. I'm not even going to think about that 
:>)  

Hope that helps,

John

> -----Original Message-----
> From: Jayaram Narayana [mailto:[EMAIL PROTECTED]
> Sent: jeudi, 28. mars 2002 04:08
> To: [EMAIL PROTECTED]
> Subject: RE: NullPointerException: appendChild()
> 
> 
> hi john,
> 
> i am definitely missing something fundamental here.
> 
> i did the setContentAsDOM as you said, and there's this 
> runtime exception:
> 
> XML:DB Exception occured 1 Collection Document Malformed
> 
> this is the code fragment:
> 
>               :
>               :
>          Database database = (Database) c.newInstance();
>          DatabaseManager.registerDatabase(database);
>          col = 
> DatabaseManager.getCollection("xmldb:xindice:///db/misc");
>          XMLResource xmlResource = (XMLResource) 
> col.getResource("test");
>          Node node = xmlResource.getContentAsDOM();
>          Document doc = node.getOwnerDocument();
>          Element el = doc.createElement("newchild");
>          node.appendChild(el);
>          xmlResource.setContentAsDOM(node);
>          col.storeResource(xmlResource);
> 
>          NodeList nl = node.getChildNodes();
> 
>          for (int i = 0; i < nl.getLength(); i++)
>               System.out.println("Node " + i + ": " + 
> nl.item(i).getNodeName());
>         }
>         catch (XMLDBException e)
>         {
>            System.err.println("XML:DB Exception occured " + 
> e.errorCode + "
> " +
>              e.getMessage());
>         }
>         finally
>         {
>            if (col != null) {
>               col.close();
>         }
>               :
>               :
> 
> and this is the xml file:
> 
> <?xml version="1.0"?>
> <parent>
>       <child id="123">Text goes here</child>
> </parent>
> 
> many thanks,
> -nani
> 
> -----Original Message-----
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
> Sent: Wednesday, March 27, 2002 5:14 PM
> To: [EMAIL PROTECTED]
> Subject: RE: NullPointerException: appendChild()
> 
> 
> Hi,
> 
> You need to call setContentAsDOM on the XMLResource just 
> before calling
> collection.store(resource), passing in the updated node, of course.
> 
> John
> 
> > -----Original Message-----
> > From: Jayaram Narayana [mailto:[EMAIL PROTECTED]
> > Sent: mercredi, 27. mars 2002 12:40
> > To: [EMAIL PROTECTED]
> > Subject: RE: NullPointerException: appendChild()
> >
> >
> > john,
> >
> > i have done collection.store(resource) and it is status quo.
> > not sure on
> > which object i must be calling the other method you mentioned -
> > setContent().
> >
> > thanks,
> > -nani
> >
> > -----Original Message-----
> > From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
> > Sent: Wednesday, March 27, 2002 4:44 PM
> > To: [EMAIL PROTECTED]
> > Subject: RE: NullPointerException: appendChild()
> >
> >
> > Hi Nani,
> >
> > I think you need to call setContent (or the DOM equivalent) and then
> > collection.store(resource). Or did you do that already?
> >
> > John
> >
> > > -----Original Message-----
> > > From: Jayaram Narayana [mailto:[EMAIL PROTECTED]
> > > Sent: mercredi, 27. mars 2002 12:10
> > > To: [EMAIL PROTECTED]
> > > Subject: RE: NullPointerException: appendChild()
> > >
> > >
> > > hey john,
> > >
> > > yes, i got the exact same thought as you did and tried 
> the following
> > > (fragment):
> > >
> > >                   :
> > >                   :
> > >          Database database = (Database) c.newInstance();
> > >          DatabaseManager.registerDatabase(database);
> > >          col =
> > > DatabaseManager.getCollection("xmldb:xindice:///db/misc");
> > >          XMLResource xmlResource = (XMLResource)
> > > col.getResource("test");
> > >          Node node = xmlResource.getContentAsDOM();
> > >          Document doc = node.getOwnerDocument();
> > >          Element el = doc.createElement("newchild");
> > >          node.appendChild((Node) el);
> > >          NodeList nl = node.getChildNodes();
> > >
> > >          for (int i = 0; i < nl.getLength(); i++)
> > >           System.out.println("Node " + i + ": " +
> > > nl.item(i).getNodeName());
> > >
> > >                   :
> > >                   :
> > >
> > > here's the output i get:
> > >
> > > Node 0: parent
> > > Node 1: newchild
> > >
> > > which means we were right. however, when i retrieve the
> > > document thru the
> > > console admin, i cannot see "newchild"
> > >
> > > now what could be wrong? is there some commit i need to do on
> > > the database?
> > >
> > > thanks,
> > > -nani
> > >
> > > -----Original Message-----
> > > From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]
> > > Sent: Wednesday, March 27, 2002 4:12 PM
> > > To: [EMAIL PROTECTED]
> > > Subject: RE: NullPointerException: appendChild()
> > >
> > >
> > > Hi Nani,
> > >
> > > I don't know if this is relevant, but cloneNode returns a
> > > copy with a parent
> > > value of null. Since nodes added to other node structures
> > > need to have the
> > > same parent, this might be your problem. Try using
> > > getOwnerDocument() to get
> > > the parent, then create a new element to append.
> > >
> > > John
> > >
> > > > -----Original Message-----
> > > > From: Jayaram Narayana [mailto:[EMAIL PROTECTED]
> > > > Sent: mercredi, 27. mars 2002 11:11
> > > > To: 'Xindice Dev. Group'
> > > > Subject: NullPointerException: appendChild()
> > > >
> > > >
> > > > hi!
> > > >
> > > > when i try to run this code against the xml given below i get
> > > > the following
> > > > exception:
> > > >
> > > > Has Children: true
> > > > Exception in thread "main" java.lang.NullPointerException
> > > > at
> > > > org.apache.xindice.xml.dom.ContainerNodeImpl.appendChild(Unkno
> > > > wn Source)
> > > >
> > > > the code fragment is:
> > > >                 :
> > > >                 :
> > > >          Database database = (Database) c.newInstance();
> > > >          DatabaseManager.registerDatabase(database);
> > > >          col =
> > > > DatabaseManager.getCollection("xmldb:xindice:///db/misc");
> > > >          XMLResource xmlResource = (XMLResource)
> > > > col.getResource("test");
> > > >          Node node = xmlResource.getContentAsDOM();
> > > >            System.out.println("Has Children: " +
> > > node.hasChildNodes());
> > > >          Node newNode = node.cloneNode(true);
> > > >          node.appendChild(newNode);
> > > >                 :
> > > >                 :
> > > >
> > > > the xml file is:
> > > >
> > > > <?xml version="1.0"?>
> > > > <parent>
> > > >         <child id="123">Text goes here</child>
> > > > </parent>
> > > >
> > > > i am not able to figure out what i am doing wrong. any help
> > > > will be greatly
> > > > appreciated.
> > > >
> > > > thanks in advance,
> > > > -nani
> > > >
> > > >
> > >
> > >
> >
> >
> 
> 

Reply via email to