Java--Xml Modify Node Value



Share on Google+Share on Google+

Java--Xml Modify Node Value
4 Answer(s)      7 years ago
Posted in : Java Beginners
View Answers

December 9, 2009 at 12:58 PM

Updating xml node is not sufficient to update that xml. You need to prepare whole xml again after updating the node. Use the following method to update xml.
* Writes the Document object into an XML file.
public static void createXMLFile(Document doc, File fileName)
OutputFormat format = new OutputFormat(doc);
XMLSerializer output = new XMLSerializer(new FileOutputStream(fileName), format);
System.out.println("File updated");
catch(Exception e){

Call this method after updationg node is completed.

Try this. I think this will help you.

December 9, 2009 at 1:26 PM

Thanks for your reply...its not working....Actually I could not even able to retrieve the element name inside the for loop...I am going wrong in the following lines..please correct me

NodeList list = doc.getElementsByTagName("*");

for (int i=0; i<list.getLength(); i++) {
// Get element
Element element = (Element)list.item(i);

No Elements are getting printd to Console..Please help me in this regard...

December 9, 2009 at 4:56 PM


The above code works if the updation is successful. That code is not for getting the node values.

Whenever you are trying to update a node, first you need to check whether it is text node or element node.
If xml is like: <node1>
<node2>sample text</node2>

Here node2 is text node and node1 is element node.

setTextcontent() is for updating value in text nodes only. If you do this for element nodes , all nodes under element node are deleted and replaced with specified text content.

Here you are using:
//The following loop iterates only once even though you are gettting node list length 5.
for (int i=0; i<list.getLength(); i++) {
Node element = (Node)list.item(i); //When i=0, This gives root node object <or:AddOrder> as as single node
System.out.println("ele: "+element.getNodeName()); //This gives output as <or:AddOrder> as the first node is <or:AddOrder>
element.setTextContent("Prasanna"); //With this line, all nodes under <or:AddOrder> suppressed and replaced with Prasanna text ie., it changed as : <or:AddOrder>Prasanna</or:AddOrder>. So it wont iterate further.

Here you need put a condition and set text as follows: Replace the above line with following code:

if(element.getNodeName().equals("BuyersID") || element.getNodeName().equals("utcc:IssueDateTime") || element.getNodeName().equals("utcc:KCIType"))
System.out.println("node name: "+element.getNodeName()+"node text:"+element.getTextContent());
//Now set text content as u done above
element.setTextContent("Prasanna"); //This replaces text content for all the mentioned nodes. If you dont need that, put each node name in separate 'if' and do as you need for each node.


Instead of iterating all nodes using *, better getting the node list by specifying tag name.

For example,
if you want to retrieve the values of <BuyersID>,<utcc:IssueDateTime>,and <utcc:KCIType> through NodeList use the following code:

If you know the xml content( ie., if xml is not getting prepared at runtime) before accessing it, try to retrieve the node values as below:
//Getting all <BuyersID> nodes.

NodeList list = doc.getElementsByTagName("BuyersID");

for (int i=0; i<list.getLength(); i++) {
System.out.println("list lenght: "+list.getLength());
// Get element
Node element = (Node)list.item(i);
System.out.println("node Name:: "+element.getNodeName()+" node text content:"+element.getTextContent()); //Here you will get values as <BuyersID> is text node.

//Update the node here if you want.

December 9, 2009 at 6:19 PM

Really thanks a lot for your patient reply.....Its working now...

I wish you a bright professional career ahead...

Once again thanks a lot...


Related Tutorials/Questions & Answers:
Java--Xml Modify Node Value - Java Beginners
Java--Xml Modify Node Value  I have the following xml. test..., Updating xml node is not sufficient to update that xml. You need to prepare whole xml again after updating the node. Use the following method to update xml
Java-Xml -Modify and Update Node - Java Beginners
Java-Xml -Modify and Update Node   test_final_1 2009-025T13:23...(); } } I want to modify the values of and in the above xml posted and update...(); System.out.println("XML Data: "); DefaultHandler dHandler = new
Java Xml -Node retrieval - Java Beginners
Java Xml -Node retrieval  I have the following xml test_final_1 2009-025T13:23:45 B2B In the above xml,I want to retrieve the value of and (ie) test_final_1,2009-025T13:23:45 respectively. I have
Java get Node Value
Java get Node Value       In this section, you will learn how to obtain the node value... of this node. getNodeValue()- This method returns the node value. Here
Determining If a Preference Node Contains a Specific Value
node 'Java Types' contains the specified value 'Hello' or not. If it finds... Determining If a Preference Node Contains a Specific Value... or not by enumerating the key/value pairs in a preference node and checks each value for a match
Java XML modification
Java XML modification This section explain you how to modify the xml file..., DocumentBuilder and Document classes to get the default DOM parser and parse the xml file. Then call the function delNode() that finds the specified node from the node
modify program
modify program  Hi how could I modify this program so that if I say yes for the dialog the following prompt would be displayed for the user to enter the information and would stop if no is selected.As the program is now if I say
Node class
Node class   hii, What is a node class?   hello, A node class is a class that has added new services or functionality beyond the services inherited from its base class
java - XML
in the XML document while i am parsing the file using SAX event based parser. In the method startElement(), i would to like to add the attribute value to the element.... provide the way in java.  Hi Friend, Try the following code: import
Java-XML-DOM  Hi! I need some help. I have to make java program that loads an xml file and from it builds DOM(later i will have to work with it - like using xpath in java find some value and replace it...). Since i'm new to java
java with xml parsing - Java Beginners
java with xml parsing  Hi, I need the sample code for parsing complex data xml file with java code. Example product,category,subcategory these type of xml files and parse using java. Please send the code immediately its very
how to modify page?
how to modify page?  Dear frnds, how to modify existing QuestionServlet. java file
a particular node within XML document" . Explanation of program:- input must be in XML document and the processing in JAVA program . Find the time for each XML element...XML with JAVA  Hi.. This is Priya here. Thanks for responding me. I
Read XML in java - XML
Read XML in java  Hi Deepak, I want to read a xml file which have... a java code is given below. import; import...; nodeLst.getLength(); s++) { Node fstNode = nodeLst.item(s); Node
Java-XML Xerces/Jaxp
Java-XML Xerces/Jaxp  Im new to Java DOM development. I have doubt or I am bit unclear about fact of JAXP and Xerces. My question is why developer use xerces when jaxp does the job
Java-XML Xerces/Jaxp
Java-XML Xerces/Jaxp  Im new to Java DOM development. I have doubt or I am bit unclear about fact of JAXP and Xerces. My question is why developer use xerces when jaxp does the job
Java XML modification - Java Beginners
Java XML modification  hey I want to delete the nodes in Xml file & that should be updated in xml is what i have tried. import...; listofmenus.getLength(); i++) { Node testcase = listofmenus.item(i); NodeList nodelist1
date_modify() in PHP date_modify or DateTime::modify function modifies... by strtotime(). It returns the modify date time. Description public DateTime DateTime::modify ( string $modify ) DateTime date_modify ( DateTime $object
Xml append node problem
Xml append node problem   print("code sample");Question: I create... to Node. Output: 111 ---> <?xml version="1.0" ?><cwMin>31<..." which is of type org.w3c.dom.Node. and the value of "nd" is : <?xml version
Version of com.nfbsoftware>java-xml dependency
List of Version of com.nfbsoftware>java-xml dependency
Version of com.nfbsoftware>java-xml dependency
List of Version of com.nfbsoftware>java-xml dependency
Java Xml Data Store
Java Xml Data Store  I have to do this project and i'm finding it so hard its way over my head if any one has any ideas or help. What would the best... or XML file. As a good Java programmer you will use correct Object-Oriented
Listening for Changes to Preference Values in a Preference Node
emits an XML document representing all the preferences contained in the node... Listening for Changes to Preference Values in a Preference Node... to change the Preference values in a Preference node. You can see in the given
Java XML parser
Java XML parser  Hi friends, i am new to java XML parsing. i need to parse the following xml response which i got from a url hit: <response> <status>SUCCESS</status> <app-config> <CHECK_LOW_SCORE>
XML in java - XML
XML in java  Write a program using SAX that will count the number of occurrences of each element type in an XML document and display them... argument.Then modify the program resulting from the this exercise so that it will accept
java and xml problem - XML
java and xml problem  hi, i need to write a java program that generates an xml file as follows: aaa vvv --------- i have witten a program in java, but im
how to store,retrieve,modify the data
how to store,retrieve,modify the data  hello sir ,how to store,retrieve,modify the data using the swing please help me
Java + XML - XML
java...the attribute value..not sure now how to read the xml file passing...Java + XML  1) I have some XML files, read one xml..." pointing to a hello xml file that file and get the value of the parent
how to read values from java in xml?
how to read values from java in xml?  how to read values from java in xml
tree menu delete node
nodes now i want to delete parent node and also want to delete sub nodes when parent node is deleted........i am using servlet and giving nodeid to servlet using url ....i have a problem when i perform a delete operation on parent node
Modify Data Type with ALTER Command
Modify Data Type with ALTER Command  Is it possible to modify... EmpName INT; Is it possible to modify the above table like... the following query: ALTER TABLE Emp MODIFY EmpName int(255