Click to See Complete Forum and Search --> : How to change a value in XML node?
ShaReB
12-02-2008, 07:24 AM
After creating the document object, I tried this way
public void changeContent(Document doc,String newemail) {
Element root = doc.getDocumentElement();
NodeList rootlist = root.getChildNodes();
Element person = (Element)rootlist.item(1);
NodeList personlist = person.getChildNodes();
Element email = (Element)personlist.item(1);
NodeList emaillist = email.getChildNodes();
Text emailtext = (Text)emaillist.item(0);
emailtext.setData(newemail);
}
but it wasn't working, is there a better way to locate and change the value of an element?
Thanks in advance.
Khalid Ali
12-02-2008, 12:57 PM
are u sure at index 0 its a text element?
ShaReB
12-02-2008, 04:21 PM
are u sure at index 0 its a text element?
yes, here's the sample xml file:
<?xml version="1.0" standalone="yes"?>
<folks>
<person>
<name>Sam Spade</name>
<email>samspade@website.com</email>
</person>
<person>
<name>Sam Diamond</name>
<email>samdiamond@website.com</email>
</person>
<person>
<name>Sam Sonite</name>
<email>samsonite@website.com</email>
</person>
</folks>
Khalid Ali
12-02-2008, 04:32 PM
fair enough, however, could you just do a debug statement and print what you get at the index 0 and then post your results here
ShaReB
12-02-2008, 05:20 PM
fair enough, however, could you just do a debug statement and print what you get at the index 0 and then post your results here
Thanks for your reply
ok, I printed the toString() of the emailtext and the result was:
[#text: Sam Spade]
Khalid Ali
12-02-2008, 05:31 PM
ok that proves one point that you are not changing the text of the correct node(I say this because of your variable name is emailtext ),
however, as I see lots of things have been changed..:-)
I was looking at the API and it seems like you can use the following method for the Text object
replaceWholeText("new text");
try this
P.S I used Java and XML about 8 yrs ago..heheh
ShaReB
12-02-2008, 05:49 PM
ok that proves one point that you are not changing the text of the correct node(I say this because of your variable name is emailtext ),
however, as I see lots of things have been changed..:-)
I was looking at the API and it seems like you can use the following method for the Text object
replaceWholeText("new text");
try this
P.S I used Java and XML about 8 yrs ago..heheh
well, yeah, you're right, lets say I want to change that text node value Sam Spade the child of name node .. I can't find the replaceWholeText method there's only replaceChild(Node newChild, Node oldChild) and replaceData(int offset, int count, String arg)
any other suggestions?
P.S that is pretty long time.
Khalid Ali
12-02-2008, 08:44 PM
what version of JDK you are using?
replaceWholeText should be in >=1.5
ShaReB
12-03-2008, 04:08 AM
what version of JDK you are using?
replaceWholeText should be in >=1.5
I'm using JDK 1.6.0 :confused:
chazzy
12-03-2008, 07:13 AM
what happens with
Element email = (Element)personlist.item(1);
email.setNodeValue(newemail);
?
ShaReB
12-03-2008, 04:06 PM
what happens with
Element email = (Element)personlist.item(1);
email.setNodeValue(newemail);
?
The text is not changed.
chazzy
12-03-2008, 05:55 PM
do you have code that writes the new doc to a file? or are you just looking at the value in memory?
Khalid Ali
12-03-2008, 07:52 PM
I am sure its in memory......
ShaReB
12-04-2008, 02:34 PM
do you have code that writes the new doc to a file? or are you just looking at the value in memory?
Yeah, I'm looking at it in memory.
Khalid Ali
12-04-2008, 06:36 PM
following code does change the value and prints it on console, I used your sample xml file
if(elRec.item(3).getNodeName().equals("email")){
System.out.println("before change["+elRec.item(3).getLastChild()+"]") ;
elRec.item(3).getLastChild().setNodeValue("myemail@emal.com");
System.out.println("after change["+elRec.item(3).getLastChild()+"]") ;
}
I tested it and it works as intended, however if you want this to persist then yo will need to write the new document to a file
ShaReB
12-09-2008, 06:32 AM
following code does change the value and prints it on console, I used your sample xml file
if(elRec.item(3).getNodeName().equals("email")){
System.out.println("before change["+elRec.item(3).getLastChild()+"]") ;
elRec.item(3).getLastChild().setNodeValue("myemail@emal.com");
System.out.println("after change["+elRec.item(3).getLastChild()+"]") ;
}
I tested it and it works as intended, however if you want this to persist then yo will need to write the new document to a file
Thanks, and it is working changing the value but Yes, I want the value to be changed in the file..
so, should I delete the old file and write a new one with the new values using I/O streams?
Khalid Ali
12-09-2008, 12:17 PM
yes rewrite the data in the file (make sure you overwrite) and I think XML api has a class to write the data into a file as well.