Home Answers Viewqa XML how to update xml from java

 
 


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

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 Pages:
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
XML
XML  How i remove a tag from xml and update it in my xml
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 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 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
Hibernate update Method
a simple example which will demonstrate you how an update method is used...Hibernate update Method In this tutorial you will learn about the update method in Hibernate Hibernate's update method saves the modified value
update a JTable - Java Beginners
update a JTable   how to update a JTable with Mysql data through user... = stat.executeQuery("SELECT * FROM register"); ResultSetTableModel model...://www.roseindia.net/java/example/java/swing/ Thanks
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
update a JTable - Java Beginners
update a JTable   i have tried your advice as how to update a JTable...); ResultSet rs = st.executeQuery("SELECT * FROM employee1... be referenced from a static contex t ResultSetTableModel model = new
How to create XML file - XML
()); }}Thanks  Hi,Java programming language has many API's to create xml file from...How to create XML file  Creating a XML file, need an example. Thanks... creating your file save it with .xml extension.  Hi,Java programming
create a xml from sql server 2005 - XML
create a xml from sql server 2005  hello Dear, i want to know how we create a xml file which retrieve data from Sql server 2005 using java. i... with the name as 'student' i want to create a xml file from the table values which
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
java - XML
java  How to prepare an xml document by reading data from a table in the database . (the database access logic is written inside the servlet
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 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
update query
update query  using oops concept in php.. How to update the data from databse ? with program example
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
XML Tutorial
XML and HTML. You will also learn how to start using XML in your applications... was designed to display data and to focus on how data looks. XML tags... tutorials you need, from basic HTML and XHTML to advanced XML, SQL, Database
update
update  how can i update multiple records in database using jsp ,servlet and jdbc based on selection of checkbox in jsp
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 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
iBatis Update -Updating data of a table
; </sqlMap> Now we can execute update command from our java... will introduce you how you can update data in data table with the iBatis. In iBatis... iBatis Update -Updating data of a table   
xml
xml  how to creatte html file and validate using java and finally i need get web.xml file
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
Hibernate Update Query
; In this tutorial we will show how to update a row with new information... write a java class to update a row to the database. Create a java class: Here is the code of our java file (UpdateExample.java), where we will update a field
how to update the text file?
how to update the text file?  my textfile with name list.txt: Rice... 150 .... ....20 lines. java code: import java.io.*; import java.util.*; class... { fr.close(); sc.close(); } }} now i want to update the quantity
xml - XML
xml  how to match to xml file?can you give example with java code
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
XML Schema
XML Schema  Good noon. Please inform me how can we send the data from xml fie to java class as an object and Vice-Versa. And wich technology we can use. Thank you
XML Books
to learn how to read and write XML documents from their code. The paper version...; Processing XML with Java Welcome to Processing XML with Java, a complete tutorial about writing Java programs that read
JDBC: Update Records Example
JDBC: Update Records Example In this section, you will learn how to update records of the table using JDBC API. Update Records : Update record is most... for such situation you can use update statement to update particular value of record
Loading properties from a XML file
Loading properties from a XML file       This Example shows you how to Load properties from a XML file. JAXP (Java API for XML Processing) is an interface which provides parsing
How to use update and reset method of CRC32
How to use update and reset method of CRC32.In this tutorial, we will discuss about the update() and reset() method of CRC32 class. The CRC32 class... in  java.util.zip package .  In this example, you will see how
XML remove tags
XML remove tags  how i update a tag from xml file
Hibernate update Query
Hibernate update Query In this tutorial you will learn about an update query in Hibernate This tutorial is based on how to write HQl update query in hibernate. In hibernate update query updates the data of the database table when
Reading XML from a File
Reading XML from a File       This Example shows you how to Load Properties from the XML file via a DOM document. JAXP (Java API for XML Processing) is an interface which provides
xml file creation in java
xml file creation in java  how to create xml file in java so...; Please visit the following links: http://www.roseindia.net/tutorial/java/xml...;Please visit the following links: http://www.roseindia.net/tutorial/java/xml
JDBC Batch Update Example
; } Batch Update Example You can update data in a table batch. To update...(updateQuery1); and finally commit the connection. An example of batch update...); statement.executeBatch(); String query = "SELECT * FROM student"; ResultSet rs
turn the GUI(java class) into XML - XML
turn the GUI(java class) into XML   Hi im trying to turn the GUI(java class) into XML ,means in my gui one combobox is there if i select one of the option that shoulb be update in xml.upto now we are doing that one manually.In
how to update
how to update   conditional update
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 specific row in on update key in the Navicat for mysql trigger
how to update specific row in on update key in the Navicat for mysql trigger   Blockquote insert into two(name, date) select name, curdate() from one on duplicate key update name=values(name
xml - XML
xml  hi convert xml file to xml string in java ,after getting the xml string result and how to load xml string result? this my problem pls help..."); FileOutputStream output = new FileOutputStream("new.xml"); ReadXML xml = new
creating index for xml files - XML
30-50 records. I would like to retrieve that xml files from the directory one.... and that directory will be in Tomcat server. I will first get tag from xml file and then tag. so (xml file name) will be get from tag for every xml file. So

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.