Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Answers Viewqa Java-Beginners Java--Xml Modify Node Value
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


prasanna.b93
Java--Xml Modify Node Value
4 Answer(s)      4 years and 10 months ago
Posted in : Java Beginners



Advertisement
View Answers

December 9, 2009 at 12:58 PM


Hi,
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)
{
try
{
OutputFormat format = new OutputFormat(doc);
format.setIndenting(true);
XMLSerializer output = new XMLSerializer(new FileOutputStream(fileName), format);
output.serialize(doc);
System.out.println("File updated");
}
catch(Exception e){
e.printStackTrace();
}
}

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);
System.out.println(element.getNodeName());

element.setTextContent("Prasanna");
System.out.println(element.getNodeName());
}
}
No Elements are getting printd to Console..Please help me in this regard...


December 9, 2009 at 4:56 PM


Hi,

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>
</node1>

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
Advertisements
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
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
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
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
how to modify QuestionServlet.java page?
how to modify QuestionServlet.java page?  Dear frnds, how to modify existing QuestionServlet. java file
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
date_modify
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
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
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
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
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 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
program to create node.
program to create node.  Hi i'm new to flex and trying to create a node and it should also have an option of right click wit any properties. how do i do this. Also i need to create a gui having drag and dop options. Please help
Java-XML-DOM - XML
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
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
how to modify content of XML file
how to modify content of XML file  hi, I want to modify content of a xml file.There are two tags of similar name say <ContentName>C:\Myinfo... to xml file to replace "XYZ".Every time with the content of text box the file should
Creating a Preference Node
Creating a Preference Node       This section demonstrates you to create a Preference Node... Node and System Preference Node. Here we are going to create a user preference
XML with JAVA - XML
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
Retrieving a Preference Node
Retrieving a Preference Node       This section demonstrates you to retrieve a Preference node. You... the Preference node. The absolute paths begin with '/'. The method name() returns
Removing a Preference from a Preference Node
(key-value associations) from the preference node. Here is the code... Removing a Preference from a Preference Node  ... the preference from a preference node. You can see in the given example
Replacing a Node with a New One
node in a DOM document. JAXP (Java API for XML Processing) is an interface which..., fchild):-This method replaces a child node with another. Xml code... Replacing a Node with a New One     
Read XML in java - XML
Read XML in java  Hi Deepak, I want to read a xml file which have... a solution.my java code is given below. import java.io.File; import...; nodeLst.getLength(); s++) { Node fstNode = nodeLst.item(s); Node
can modify this application connection with database
can modify this application connection with database   hi i'm want to say who thank java master or java professional help me convert Java applet to Java Frame, in here can i ask one more question about using this application
Removing a Preference Node
Removing a Preference Node       This section demonstrates you to remove the Preference node... node using the method userRoot().node("/Roseindia Employees") method
Creates element node, attribute node, comment node, processing instruction and a CDATA section
,Attribute node, Processing node and CDATA section node in a DOM document. JAXP (Java... for the node Xml code for the program generated is:- <... Creates element node, attribute node, comment node, processing
Replacing a Text Node with a New CDATA Section Node
()):-replaces a child node direction with CDATASection node. Xml code... Replacing a Text Node with a New CDATA Section Node... to replace a Text node with new CDATASection Node in a DOM document. Methods which
java.lang.IllegalArgumentException: node to traverse cannot be null!
java.lang.IllegalArgumentException: node to traverse cannot be null!  Can Any body help me out regarding the above exception in Hibernate HQL
Advertisements
 

 

 

DMCA.com