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



Home Answers Viewqa XML how to update xml from java

Have Programming Question? Ask it here!
 
 
 


srikala
how to update xml from java
3 Answer(s)      5 years and 5 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 pretty print XML from Java?
How to pretty print XML from Java?  How to pretty print XML from Java
Advertisements
how to read values from java in xml?
how to read values from java in xml?  how to read values from java in xml
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 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 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 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 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 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 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
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... 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
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 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 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 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
how to write java data - XML
how to write java data  how to write data to xml file  Hi friend, Read for more information, http://www.roseindia.net/xml/dom/ Thanks
How to write to xml file in Java?
How to write to xml file in Java?  Hi Friends, Can anyone help me how to write in xml file in Java programming. Please fill free to give example or reference website for getting example.   Hi, To write in xml file
Update Database from jsp
Update Database from jsp   I want to update my oracle database column from a text box ,so whenever I input some text value in the text box and click UPDATE button the database field should be updated . I have a drop down menu
download xml file from website using java code
download xml file from website using java code  how to download xml file from website using java code
how to update image in php
how to update image in php  how to update image in php
update date from stored procedure
update date from stored procedure  how to works on insert,update data from stored procedure.   Insert data using stored procedure
How to parse the attributes from XML using KXML parser in j2me
How to parse the attributes from XML using KXML parser in j2me  Dear Sir, I have doubt in How to parse the attributes from XML using KXML... from xml...please help me for parsing the attributes in j2me... Regards Selva
how to update values of a html form into an excel table using java servlets?
how to update values of a html form into an excel table using java servlets?  i have written a java servlet program, which has a html form to be filled. after filling the form the servlet generates a receipt and the values should
how to update the text file?
how to update the text file?  if my text file contains a string and integer in each line say,: aaa 200 bbb 500 ccc 400 i need a java code to update the integer value if my input String matches with the string in file. please
update count from session - Hibernate
update count from session  I need to get the count of how many rows got updated by session.saveOrUpdate(). How would I get this? Thanks, mpr
how to read and write an xml file using java
how to read and write an xml file using java  Hi Can anyone help me how to read and write an xml file which has CData using java
How to Split a large XML file using java?
How to Split a large XML file using java?  How can we split a 500MB Xml file?I know how to split xml file after reading the entire document in a file.Here we cannot load the entire file as it is a large file
how to convert text file to xml file in java. - XML
how to convert text file to xml file in java.  Hi all, I m... the no of record(i.e no of different line of information)to a xml file through java...://www.roseindia.net/java/java-conversion/TextToXML.shtml Hope that it will be helpful
solution to get values for xml namespace tags from sqldatabase in java
solution to get values for xml namespace tags from sqldatabase in java  hi I have written a code in java to get these xml tags < company comp:loc... field(" "). how do i get these values.. already connected database and created xml
Want solution to get values for xml namespace tags from database in java
Want solution to get values for xml namespace tags from database in java  hi I have written a code in java to get these xml tags < company comp:loc=" "/> now i need to get values from sql database for values in the blank
How to update table in Hibernate
How to update table in Hibernate  Hi, I have a table in database that has two fields in it. Student Name and ID, can anyone explain me how to update these tables in Hibernate. Thanks.   Update table query in Hibernate
how to update the value of jslider
how to update the value of jslider  hello, I want to make a audio player but the jslider is not updating help me
how to update the value of jslider
how to update the value of jslider  hello, I want to make a audio player but the jslider is not updating help me
how to update the value of jslider
how to update the value of jslider  hello, I want to make a audio player but the jslider is not updating help me
Advertisements
 

 

 

DMCA.com