[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: Updating / replacing the content of a Container



Thanks for helping out Lars.
Here's the usage:

    private void updateDOMDocument(String id) throws Exception {
        try {
            XMLResource resource = (XMLResource) col.getResource(id);

            Document document = (Document) resource.getContentAsDOM();
            Element root = document.getDocumentElement();
            // Change document by appending to an element data

            Text nameNode = document.createTextNode(" updateAddition");
            NodeList list = root.getElementsByTagName("testName");
            Node parent = list.item(0);
            parent.appendChild(nameNode);

            // insert a new element
            list = root.getElementsByTagName("levelZeroTests");
            Node levelZeroTests = list.item(0);
            Element testName = document.createElement("testName");
            levelZeroTests.appendChild(testName);
            Node name = document.createTextNode("testSAX");
            testName.appendChild(name);

            resource.setContentAsDOM(document);   // here's where it fails
            col.storeResource(resource);
        }
        catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
   }

Here is the method that fails. I need to find a way to replace the content
for the container with the new one. Deleting and recreating the container
looks ugly i think, maybe there's a better way?

    public void setContentAsDOM( Node content ) throws XMLDBException {
        ExternalTransaction tx = database.newTransaction();
        try {
            if (content == null) {
                throw new XMLDBException(ErrorCodes.INVALID_RESOURCE);
            }
            if (content instanceof Document) {
                tx.begin();
                container.storeDOM((Document)content); // container is an
XMLContainer
                tx.commit();
            }
            else {
                throw new XMLDBException(ErrorCodes.VENDOR_ERROR,"NOT
IMPLEMENTED -
			Cannot store Nodes right now, must be a Document");
            }
        }
        catch (Exception e) {
            try {
                if (tx.getStatus() == tx.STATUS_ACTIVE)
                    tx.rollback();
                throw new XMLDBException( ErrorCodes.VENDOR_ERROR,
e.getMessage());
            }
            catch (Exception rollbackException) {
                throw new XMLDBException(ErrorCodes.VENDOR_ERROR,
rollbackException.getMessage());
            }
        }
    }

Hope this clarifies it.

Best regards,
Per
> -----Original Message-----
> From: owner-ozone-dev@ozone-db.org
> [mailto:owner-ozone-dev@ozone-db.org]On Behalf Of Lars Martin
> Sent: den 25 juli 2001 10:52
> To: ozone-dev@ozone-db.org
> Subject: Re: Updating / replacing the content of a Container
>
>
> On Mon, 23 Jul 2001 18:27:58 +0200
> "Per Nyfelt" <per.nyfelt@nordicwave.com> wrote:
>
> > Does anyone have a good suggestion for how to replace (not
> append) content
> > for a container? I have a situation as follows:
> >
> > 1. I get the DOM Document from the XMLContainer
> > 2. Add some stuff to the Document
> > 3. Try to update the content using storeDOM(theDocument) but get the
> > following Exception:
>
> Hi Per.
>
> Can you send a piece of code? AFAIK, the storeDOM() method is not
> intended to perform updates on a given Node/Document but for storing
> a whole newly created Document. This should be the reason of your
> Exception.
>
> Regards,
> Lars
>
> > java.lang.RuntimeException: The given DOM document must not
> have children if
> > a whole document shall be converted!
> >         at
> >
> org.ozoneDB.xml.util.XMLContainerHelperImpl_Proxy.putChunk(XMLCont
> ainerHelpe
> > rImpl_Proxy.java:220)
> >         at
> org.ozoneDB.xml.util.XMLContainer.storeDOM(XMLContainer.java:234)
> >         at
> org.ozoneDB.xml.util.XMLContainer.storeDOM(XMLContainer.java:181)
> >         at
> >
> org.ozoneDB.xml.cli.resources.XMLResourceImpl.setContentAsDOM(XMLR
> esourceImp
> > l.java:216)
> >         at LevelZeroTest.updateDOMDocument(LevelZeroTest.java:270)
> >         at LevelZeroTest.testDOM(LevelZeroTest.java:157)
> >         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> >         at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
> >         at
> sun.reflect.InflatableMethodAccessorImpl.invoke(Unknown Source)
> >         at java.lang.reflect.Method.invoke(Unknown Source)
> >         at junit.framework.TestCase.runTest(TestCase.java:156)
> >         at junit.framework.TestCase.runBare(TestCase.java:130)
> >         at junit.framework.TestResult$1.protect(TestResult.java:106)
> >         at junit.framework.TestResult.runProtected(TestResult.java:124)
> >         at junit.framework.TestResult.run(TestResult.java:109)
> >         at junit.framework.TestCase.run(TestCase.java:121)
> >         at junit.framework.TestSuite.runTest(TestSuite.java:160)
> >         at junit.framework.TestSuite.run(TestSuite.java:155)
> >         at junit.framework.TestSuite.runTest(TestSuite.java:160)
> >         at junit.framework.TestSuite.run(TestSuite.java:155)
> >         at junit.framework.TestSuite.runTest(TestSuite.java:160)
> >         at junit.framework.TestSuite.run(TestSuite.java:155)
> >         at junit.textui.TestRunner.doRun(TestRunner.java:74)
> >         at junit.textui.TestRunner.run(TestRunner.java:201)
> >         at XMLDBTestSuite.main(XMLDBTestSuite.java:60)
> >
> > All suggestions are most welcome.
> >
> > Best regards,
> > Per
> >
> > ----------------------------------------------------------------------
> > Post a message:         mailto:ozone-dev@ozone-db.org
> > Unsubscribe:
> mailto:ozone-dev-request@ozone-db.org?body=unsubscribe
> > Contact adminstrator:   mailto:ozone-dev-owner@ozone-db.org
> > Read archived messages: http://www.ozone-db.org/
> > ----------------------------------------------------------------------
>
>
> --
> ______________________________________________________________________
> Lars Martin                                    mailto:lars@smb-tec.com
> SMB GmbH                                        http://www.smb-tec.com
>
> ----------------------------------------------------------------------
> Post a message:         mailto:ozone-dev@ozone-db.org
> Unsubscribe:
> mailto:ozone-dev-request@ozone-db.org?body=unsubscribe
> Contact adminstrator:   mailto:ozone-dev-owner@ozone-db.org
> Read archived messages: http://www.ozone-db.org/
> ----------------------------------------------------------------------
>

----------------------------------------------------------------------
Post a message:         mailto:ozone-dev@ozone-db.org
Unsubscribe:            mailto:ozone-dev-request@ozone-db.org?body=unsubscribe
Contact adminstrator:   mailto:ozone-dev-owner@ozone-db.org
Read archived messages: http://www.ozone-db.org/
----------------------------------------------------------------------