You are viewing a plain text version of this content. The canonical link for it is here.
Posted to xindice-users@xml.apache.org by Tadej Lasic <tr...@siol.com> on 2005/05/14 18:20:19 UTC

Change a DOM value and save to database

Hi,

I have a question about Xindice and DOM node value manipulation. My program
needs to read an XML document from the database, read the elements and text
values and allow the user to change text values and then save the XML
document with the changed values back into the database while maintaining
the structure. I'm using Xindice 1.0 and J2SE 1.4.2.

Now, I can open and read a DOM document with Xindice using the
getContentAsDOM() method and traverse it with NodeIterator, but I don't know
how I could manipulate a certain Node.TEXT value and than store the whole
XML document into the database without changing the structure or any of the 
other values. Here's some code:

public class Xindice {
    static Node node;

    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/addressbook");

            XMLResource document = (XMLResource) col.getResource("address");

            if (document != null) {
                // Create the NodeIterator

                DocumentTraversal traversable = (DocumentTraversal)
document.
                                                getContentAsDOM();
                NodeIterator iterator = traversable.createNodeIterator(
                        document.getContentAsDOM(), NodeFilter.SHOW_ALL,
null, true);

                while ((node = iterator.nextNode()) != null) {

                    switch (node.getNodeType()) {

                    case Node.TEXT_NODE:
                        if (!node.getNodeValue().trim().equals("")) {

                            node.setNodeValue("test");
                            System.out.println(node.getNodeValue().trim());

                        }
                        break;

                    }

                } //while

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

}

As you can I try to set a new value using node.setNodeValue("test") but I
don't know how to get the changed DOM tree into the database. Any help would
be very appreciating.

Regards,
        Tadej 



-- 
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.308 / Virus Database: 266.11.10 - Release Date: 13.5.2005


Re: Change a DOM value and save to database

Posted by Tadej Lasic <tr...@siol.com>.
Hi,

Thanks, that helped. I've tried XUpdate before and always got a strange 
CORBA error message, but it works now.

Regards,
         Tadej

----- Original Message ----- 
From: "Yan Langlois" <ya...@eseo.fr>
To: <xi...@xml.apache.org>
Sent: Saturday, May 14, 2005 10:58 PM
Subject: Re: Change a DOM value and save to database


> Hi,
>
> In my last project with Xindice I had to insert/remove/update nodes 
> directly on the database. I used XUpdate to do it. Here I think you should 
> generate an XUpdate query to save the modifications. See 
> http://xml.apache.org/xindice/1.0/guide-developer.html#Using+XUpdate+to+Modify+the+Database 
> for more information.
>
> Yan.
>
> Tadej Lasic wrote:
>
>> Hi,
>>
>> I have a question about Xindice and DOM node value manipulation. My 
>> program
>> needs to read an XML document from the database, read the elements and 
>> text
>> values and allow the user to change text values and then save the XML
>> document with the changed values back into the database while maintaining
>> the structure. I'm using Xindice 1.0 and J2SE 1.4.2.
>>
>> Now, I can open and read a DOM document with Xindice using the
>> getContentAsDOM() method and traverse it with NodeIterator, but I don't 
>> know
>> how I could manipulate a certain Node.TEXT value and than store the whole
>> XML document into the database without changing the structure or any of 
>> the other values. Here's some code:
>>
>> public class Xindice {
>>    static Node node;
>>
>>    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/addressbook");
>>
>>            XMLResource document = (XMLResource) 
>> col.getResource("address");
>>
>>            if (document != null) {
>>                // Create the NodeIterator
>>
>>                DocumentTraversal traversable = (DocumentTraversal)
>> document.
>>                                                getContentAsDOM();
>>                NodeIterator iterator = traversable.createNodeIterator(
>>                        document.getContentAsDOM(), NodeFilter.SHOW_ALL,
>> null, true);
>>
>>                while ((node = iterator.nextNode()) != null) {
>>
>>                    switch (node.getNodeType()) {
>>
>>                    case Node.TEXT_NODE:
>>                        if (!node.getNodeValue().trim().equals("")) {
>>
>>                            node.setNodeValue("test");
>> 
>> System.out.println(node.getNodeValue().trim());
>>
>>                        }
>>                        break;
>>
>>                    }
>>
>>                } //while
>>
>>            } else {
>>                System.out.println("Document not found");
>>            }
>>        } catch (XMLDBException e) {
>>            System.err.println("XML:DB Exception occured " + e.errorCode);
>>        } finally {
>>            if (col != null) {
>>                col.close();
>>            }
>>        }
>>    }
>>
>> }
>>
>> As you can I try to set a new value using node.setNodeValue("test") but I
>> don't know how to get the changed DOM tree into the database. Any help 
>> would
>> be very appreciating.
>>
>> Regards,
>>        Tadej
>>
>>
>
>
>
> -- 
> No virus found in this incoming message.
> Checked by AVG Anti-Virus.
> Version: 7.0.308 / Virus Database: 266.11.10 - Release Date: 13.5.2005
>
> 



-- 
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.308 / Virus Database: 266.11.10 - Release Date: 13.5.2005


Re: Change a DOM value and save to database

Posted by Yan Langlois <ya...@eseo.fr>.
Hi,

In my last project with Xindice I had to insert/remove/update nodes 
directly on the database. I used XUpdate to do it. Here I think you 
should generate an XUpdate query to save the modifications. See 
http://xml.apache.org/xindice/1.0/guide-developer.html#Using+XUpdate+to+Modify+the+Database 
for more information.

Yan.

Tadej Lasic wrote:

> Hi,
>
> I have a question about Xindice and DOM node value manipulation. My 
> program
> needs to read an XML document from the database, read the elements and 
> text
> values and allow the user to change text values and then save the XML
> document with the changed values back into the database while maintaining
> the structure. I'm using Xindice 1.0 and J2SE 1.4.2.
>
> Now, I can open and read a DOM document with Xindice using the
> getContentAsDOM() method and traverse it with NodeIterator, but I 
> don't know
> how I could manipulate a certain Node.TEXT value and than store the whole
> XML document into the database without changing the structure or any 
> of the other values. Here's some code:
>
> public class Xindice {
>    static Node node;
>
>    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/addressbook");
>
>            XMLResource document = (XMLResource) 
> col.getResource("address");
>
>            if (document != null) {
>                // Create the NodeIterator
>
>                DocumentTraversal traversable = (DocumentTraversal)
> document.
>                                                getContentAsDOM();
>                NodeIterator iterator = traversable.createNodeIterator(
>                        document.getContentAsDOM(), NodeFilter.SHOW_ALL,
> null, true);
>
>                while ((node = iterator.nextNode()) != null) {
>
>                    switch (node.getNodeType()) {
>
>                    case Node.TEXT_NODE:
>                        if (!node.getNodeValue().trim().equals("")) {
>
>                            node.setNodeValue("test");
>                            
> System.out.println(node.getNodeValue().trim());
>
>                        }
>                        break;
>
>                    }
>
>                } //while
>
>            } else {
>                System.out.println("Document not found");
>            }
>        } catch (XMLDBException e) {
>            System.err.println("XML:DB Exception occured " + e.errorCode);
>        } finally {
>            if (col != null) {
>                col.close();
>            }
>        }
>    }
>
> }
>
> As you can I try to set a new value using node.setNodeValue("test") but I
> don't know how to get the changed DOM tree into the database. Any help 
> would
> be very appreciating.
>
> Regards,
>        Tadej
>
>