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



Home Answers Viewqa XML how to update xml from java
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


srikala
how to update xml from java
3 Answer(s)      5 years and 10 months ago
Posted in : XML



Advertisement
View Answers

March 25, 2009 at 2:32 AM


hi friend,

To solve your problem, please visit to:
http://www.roseindia.net/xml/



Thanks


March 25, 2009 at 5:43 AM


Hi Friend,

Here is the solution for your problem. Suppose we have one xml file named "document.xml"

document.xml
============

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Company>
<Employee>
<name>koaml</name>
</Employee>
<Employee>
<name>newsTrack</name>
</Employee>
<Employee>
<name>Girish Tewari</name>
</Employee>
</Company>

We are going to modify the value of this XML file. ModifyXML class is being used to modify the attribute value.


import java.io.*;
import java.io.BufferedWriter;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;

public class ModifyXML {

public final static void main(String[] args) throws Exception {
File file = new File("document.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

dbf.setCoalescing(true);
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(file);

ModifyXML obj = new ModifyXML ();
obj.changeValue(doc, "k1", "koaml");
obj.save(file,doc);
}

public void changeValue(Document doc, String oldValue, String NewValue) throws Exception {

Element root = doc.getDocumentElement();

NodeList childNodes = root.getElementsByTagName("Employee");

for (int i = 0; i < childNodes.getLength(); i++) {
NodeList subChildNodes = childNodes.item(i).getChildNodes();
for (int j = 0; j < subChildNodes.getLength(); j++) {
try {
if (subChildNodes.item(j).getTextContent().equals(oldValue)) {
subChildNodes.item(j).setTextContent(NewValue);
}
} catch (Exception e) {
}
}
}
}

public void save(File file ,Document doc) throws Exception {

TransformerFactory factory1 = TransformerFactory.newInstance();
Transformer transformer = factory1.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
DOMSource source = new DOMSource(doc);
transformer.transform(source, result);
String s = writer.toString();
System.out.println(s);

FileWriter fileWriter = new FileWriter(file);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);

bufferedWriter.write(s);

bufferedWriter.flush();
bufferedWriter.close();
}
}

We hope that this will help you in solving your problem.

Thanks
Rose India Team


November 29, 2011 at 4:58 PM


Thank You very much for this good tutorial.



Related Tutorials/Questions & Answers:
how to update xml from java - XML
how to update xml from java  hi, Im new to xml parsing and dont know much about. I need to modify the attribute val of a tag in a complex xml file by using java code tell me the procedure. Thanks in advance.   hi
how to read values from java in xml?
how to read values from java in xml?  how to read values from java in xml
Advertisements
How to pretty print XML from Java?
How to pretty print XML from Java?  How to pretty print XML from Java
how to create xml schema from xml doc using java
how to create xml schema from xml doc using java  i want to create xml schema from xml document programatically using java... am using Netbeans IDE 7.0 i hav created the xml document for a table of data from Database... now i
How to read value from xml using java?
How to read value from xml using java?  Hi All, I want to read value from following xml using java.. In <Line>,data is in format of key... in advance <pre lang="xml"> &lt;?xml version="1.0" encoding="UTF-8" ?>
How to values from xml using java?
How to values from xml using java?  Hi All, I want to read value from following < Line> xml using java.. In < Line>,data is in format of key and value pair.. i want to read only values..could u plz help me
How to update table column from the values of Arraylist in java
How to update table column from the values of Arraylist in java  Hii Sir, I have an arraylist containing these values [2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 3, 3, 4, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5, 5, 1, 1
How to update mysql from 5.1 to 5.5
How to update mysql from 5.1 to 5.5  How to update mysql from 5.1 to 5.5
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
How to recover from google panda update?
How to recover from google panda update?  Hi, After rolling out of the Google Panda 4.0 update my website is hit and the traffic is now more than 60... Panda update ASAP. Can anyone guide me "How to recover from google panda update
How to generate XML from XSD?
How to generate XML from XSD?  Hi Experts, I have a xsd with me. I want to generate XML files based on the XSD with fields filled out from the database tables. Please Help me out
How to create XML from Swings
How to create XML from Swings  How to create XML using Swings. I have a Swing GUI and capturing all data from it.When i click on submit, an xml...; Here is a code that accepts the data from the user through swing
how to update
how to update   conditional update
how to use update - Java Beginners
how to use update  Hi all, your all code is perfect thanks. I... taken from database but view modify and delete is link if user click modify data... button I want to click modify button then all datamodify from database
How to store extracted values from xml in a database? - XML
How to store extracted values from xml in a database?  I want to store extracted xml values in a database... How can i store extacted xml values in a database... give me a example
How to store extracted values from xml in a database? - XML
How to store extracted values from xml in a database?  I want to store extracted xml values in a database... How can i store extacted xml values in a database... give me a example
How to access the Title tag from xml to jsp
How to access the Title tag from xml to jsp  How to access the Title tag from xml to jsp   Please visit the following link: http... that will read an xml file and display the data into jsp file
How to update,Delete database values from jtable cells ..
How to update,Delete database values from jtable cells ..  Hello Sir... from database to jtable .Now as per my requirement i need to update and delete the database records from the table cells by entering new values there only
How to update,Delete database values from jtable cells ..
How to update,Delete database values from jtable cells ..  Hello Sir, I am working on a project in which i have to fetch the values from database to jtable .Now as per my requirement i need to update and delete the database
How to create one xml file from existing xml file's body?
How to create one xml file from existing xml file's body?  Hi, i'm working with content optimization system.I want to know how we can take all data from an xml doc's body to develope another xml with that content.I'm using JDOm
How to update,Delete database values from jtable cells ..
How to update,Delete database values from jtable cells ..  hello Sir... from database into jtable of a jpanel.. Now Sir, According to my need i have to update the cell values from there only means that whatever values i ma entering
How to access the following tag from xml to jsp
How to access the following tag from xml to jsp  How can i get the title where cat="1" and "My Title" from XML File?   Please visit... will provide you an example that will read an xml file and display the data
How to insert and update all column values of database from jtable.
How to insert and update all column values of database from jtable.  ... ,update,delete database values from jtable only so i added three buttons add,update,delete .. mydatabase contains five columns id,name,address,contact,email
Read the value from XML in java
Read the value from XML in java  Hi, i have an XML with the following code. I need to get the path("D... the permissions on that file.So how can i read that value. This is little urgent
How to insert and update all column values of database from jtable.
How to insert and update all column values of database from jtable.  ... ,update,delete database values from jtable only so i added three buttons add,update... added successfully..everytime... and update button is not working atall
How to insert and update all column values of database from jtable.
How to insert and update all column values of database from jtable.  ... ,update,delete database values from jtable only so i added three buttons add,update... added successfully..everytime... and update button is not working atall
How to extract details from XML? - JSP-Servlet
How to extract details from XML?  I want to extract details from http://service.openkapow.com/palanikumar/airportantigua.rest How can i extract details from this link?Here is my program... Airport
how to get this xml syntx in java??????
how to get this xml syntx in java??????  how to get this xml syntx in java?????? < comp xlink:type="new" xlink:actuate="onload"/>
how to get this xml syntx in java??????
how to get this xml syntx in java??????   how to get this xml syntx in java?????? < comp xlink:type="new" xlink:actuate="onload"/>
How to delete and update from Jtable cell in swing app
How to delete and update from Jtable cell in swing app  Hii Sir, I am developing a swing app for adding,updating and deleting from jtable... is getting removed from the jtable but selected row is getting deleted from
JDOM Element Example, How to remove child of an element from xml file in java.
JDOM Element Example, How to remove child of an element from xml file in java. In this tutorial, we will see how to remove child of an element from xml file... for manipulating, creating and serializing  xml file. With the help of this java
 

 

 

DMCA.com