dcsimg
Problem updating an XML document using setNodeValue in DOM
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Jay_K
Posted On:   Friday, December 12, 2003 03:14 AM

Hello, I am using Java to update XML documents. If my XML is as below tt Here is my Java code String name = "ddd"; Element el = tr.getNextElement(); NodeList list = el .getElementsByTagName("abc"); for (int i = list.getLength(); i>0;i--) { Node myNode = list.item(i-1); NodeList list2 = myNode.getChildNodes(); Node myNode1 = list2.item(1); if(myNode1.getFirstChild() != null) { myNode1.getFirstChild().setNodeValue(name); } else    More>>

Hello,



I am using Java to update XML documents. If my XML is as below







tt











Here is my Java code



String name = "ddd";

Element el = tr.getNextElement();

NodeList list = el .getElementsByTagName("abc");

for (int i = list.getLength(); i>0;i--)

{

Node myNode = list.item(i-1);

NodeList list2 = myNode.getChildNodes();



Node myNode1 = list2.item(1);

if(myNode1.getFirstChild() != null)

{

myNode1.getFirstChild().setNodeValue(name);

}

else

{

myNode1.setNodeValue(name);

}

}




It works fine if I am updating node 'd'. But when I update node 'c', since there is no value in it and myNode1.getFirstChild() is null, even after executing the following statement, myNode1.setNodeValue(name);, the value of node 'c' is not updated.



This is quite urgent. Can anybody please help me with this?



Thank you

   <<Less

Re: Problem updating an XML document using setNodeValue in DOM

Posted By:   Jay_K  
Posted On:   Friday, December 12, 2003 06:49 AM

I have found a solution to the problem myself.



myNode1.setNodeValue(name);

should be replaced by the following two lines of code.



Node newNode = (tr.getDocument()).createTextNode( name);


myNode1.appendChild( newNode );



Thank You
About | Sitemap | Contact