Java--Xml Modify Node Value

Ads
 

Java--Xml Modify Node Value

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...

Ads









Related Tutorials/Questions & Answers:
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 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
Advertisements
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
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
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>
how to read values from java in xml?
how to read values from java in xml?  how to read values from java in xml
Interceptors Configuration using Java or XML
In this section, you will learn about how to configure interceptors using Java or XML
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
Maven Dependency java-xml >> 1.0.6
You should include the dependency code given in this page to add Maven Dependency of com.nfbsoftware >> java-xml version1.0.6 in your project
Maven Dependency java-xml >> 1.0.7
You should include the dependency code given in this page to add Maven Dependency of com.nfbsoftware >> java-xml version1.0.7 in your project
Maven Dependency java-xml >> 1.0.5
You should include the dependency code given in this page to add Maven Dependency of com.nfbsoftware >> java-xml version1.0.5 in your project
how to modify QuestionServlet.java page?
how to modify QuestionServlet.java page?  Dear frnds, how to modify existing QuestionServlet. java file
JAVA - XML
JAVA  hi.. i want to talk to any SWT expert in JAVA... how can i do it?   Hi friend, For read more information,Examples and Tutorials on SWT visit to : http://www.roseindia.net/tutorials/swt/ Thanks
java with xml
java with xml  hi i have a problem. // this sample code is reading xml file in java DefaultHandler handler = new DefaultHandler... xml file change in future then my java code will also change. but is there any
java with xml
java with xml  Hi i am reading xml data with sax parser in java. ok its fine. But in future xsd and xml will change. now my question is if xsd and XML will change my java progrm will not change. is it possible ? Thanks
java with xml
java with xml  Hi i am reading xml data with sax parser in java. ok its fine. But in future xsd and xml will change. now my question is if xsd and XML will change my java progrm will not change. is it possible ? Thanks
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
ModuleNotFoundError: No module named 'pdfconduit-modify'
ModuleNotFoundError: No module named 'pdfconduit-modify'  Hi, My... named 'pdfconduit-modify' How to remove the ModuleNotFoundError: No module named 'pdfconduit-modify' error? Thanks   Hi, In your
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
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
how to modify hosts file in ubuntu
how to modify hosts file in ubuntu  Hi, I want to add few host names with ip address in Ubuntu. how to modify hosts file in ubuntu? Thanks  ... modify hosts file in Ubunut. Thanks
ModuleNotFoundError: No module named 'node'
ModuleNotFoundError: No module named 'node'  Hi, My Python program is throwing following error: ModuleNotFoundError: No module named 'node' How to remove the ModuleNotFoundError: No module named 'node' error
Is it modify Object data? Generally Object data can't modify............but in this program it modifies.......... Why?
Is it modify Object data? Generally Object data can't modify... integer value: "); a=Integer.parseInt(dis.readLine()); //Fundamental... conversion"); System.out.println("a+10 value: "+(a+10)+"\t\t\ta+20 value: "+(a+20
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
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
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
ModuleNotFoundError: No module named 'django-modify-history'
ModuleNotFoundError: No module named 'django-modify-history'  Hi...: No module named 'django-modify-history' How to remove the ModuleNotFoundError: No module named 'django-modify-history' error? Thanks   Hi
ModuleNotFoundError: No module named 'modify-csi-datatable'
ModuleNotFoundError: No module named 'modify-csi-datatable'  Hi...: No module named 'modify-csi-datatable' How to remove the ModuleNotFoundError: No module named 'modify-csi-datatable' error? Thanks   Hi

Ads