Java code to append/add data into a existing xml file
Java code to append data into a existing xml file,
As user enters data it overwrites the previous XML file,I want it to be append the data in XML file rather then overwriting.
Heres My code:
<%@page import="java.io.*,org.w3c.dom.*,javax.xml.parsers.*,javax.xml.transform.*, javax.xml.transform.dom.*,javax.xml.transform.stream.*,javax.xml.*"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>untitled</title>
</head>
<body>
<P>
<form>
<P>Name
<input type="text" name="text1"/>
</P>
<P>Address
<input type="text" name="text2"/></P>
<P>Contact
<input type="text" name="text3"/></P>
<P>Email
<input type="text" name="text4"/></P>
<P>
</P>
<P>
<input type="submit" value="Submit" name="submit"/>
</P>
</form></P>
<%!
public void createXmlTree(Document doc,String name,String address,String contact,String email) throws Exception {
System.out.println(name);
Element root = doc.createElement("Student");
doc.appendChild(root);
Element child1 = doc.createElement("Name");
root.appendChild(child1);
Text text1 = doc.createTextNode(name);
child1.appendChild(text1);
Element child2 = doc.createElement("Address");
root.appendChild(child2);
Text text2 = doc.createTextNode(address);
child2.appendChild(text2);
Element child3 = doc.createElement("ContactNo");
root.appendChild(child3);
Text text3 = doc.createTextNode(contact);
child3.appendChild(text3);
Element child4 = doc.createElement("Email");
root.appendChild(child4);
Text text4 = doc.createTextNode(email);
child4.appendChild(text4);
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
StringWriter sw = new StringWriter();
StreamResult result = new StreamResult(sw);
DOMSource source = new DOMSource(doc);
transformer.transform(source, result);
String xmlString = sw.toString();
File file = new File("c:/new.xml");
BufferedWriter bw = new BufferedWriter(new FileWriter(file));
bw.write(xmlString);
bw.flush();
bw.close();
}%>
<%
String name1,address1,contact1,email1;
name1 = request.getParameter("text1");
address1 = request.getParameter("text2");
contact1 = request.getParameter("text3");
email1 = request.getParameter("text4");
String name=name1;
String address=address1;
String contact=contact1;
String email=email1;
try
{
System.out.println(name);
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = builderFactory.newDocumentBuilder();
Document doc = docBuilder.newDocument();
createXmlTree(doc,name,address,contact,email);
out.println("<b>Xml File Created Successfully</b>");
}
catch(Exception e)
{
System.out.println(e);
}
%>
</body>
</html>
This JSP code overwrites XML file with new data but not appends it.I want to know how to apend that data.Thank you
View Answers
March 7, 2011 at 1:01 PM
Do modification in createxml.jsp
<%@page import="java.io.*,org.w3c.dom.*,javax.xml.parsers.*,javax.xml.transform.*, javax.xml.transform.dom.*,javax.xml.transform.stream.*"%>
<%!
public void createXmlTree(Document doc,String name,String address,String contact,String email) throws Exception{
System.out.println(name);
Element root = doc.createElement("Employee");
doc.appendChild(root);
Element child1 = doc.createElement("Name");
root.appendChild(child1);
Text text1 = doc.createTextNode(name);
child1.appendChild(text1);
Element child2 = doc.createElement("Address");
root.appendChild(child2);
Text text2 = doc.createTextNode(address);
child2.appendChild(text2);
Element child3 = doc.createElement("ContactNo");
root.appendChild(child3);
Text text3 = doc.createTextNode(contact);
child3.appendChild(text3);
Element child4 = doc.createElement("Email");
root.appendChild(child4);
Text text4 = doc.createTextNode(email);
child4.appendChild(text4);
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
StringWriter sw = new StringWriter();
StreamResult result = new StreamResult(sw);
DOMSource source = new DOMSource(doc);
transformer.transform(source, result);
String xmlString = sw.toString();
File file = new File("c:/emp.xml");
FileWriter fw=new FileWriter(file,true);
BufferedWriter bw = new BufferedWriter(fw);
bw.write(xmlString);
bw.flush();
bw.close();
}
%>
<%
String name=request.getParameter("name");
String address=request.getParameter("address");
String contact=request.getParameter("contact");
String email=request.getParameter("email");
try{
System.out.println(name);
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = builderFactory.newDocumentBuilder();
Document doc = docBuilder.newDocument();
createXmlTree(doc,name,address,contact,email);
out.println("<b>Xml File Created Successfully</b>");
}
catch(Exception e){
System.out.println(e);
}
%>
March 7, 2011 at 1:39 PM
Sorry sir but your code is not creating XML file,and it is same as that of my code, I havent find emp.XML fileusing ur code.Please check it aain and answer it.Thank you
May 18, 2011 at 4:18 PM
That code is very good.
I need to add node in exist file but I can not help me because I am searching it.
Related Tutorials/Questions & Answers:
Java code to append/add data into a existing xml fileJava code to
append/
add data into a
existing xml file
Java code to
append data into a
existing xml file,
As user enters
data it overwrites the previous
XML file,I want it to be
append the
data in
XML file rather then overwriting
Advertisements
data retrivel code - XMLdata retrivel code Can someone help me in retriving
data from MySql database into an
XML file using
java. Hi Shruti,
Please visit.../servlets/login-
Xml-servlet.shtml
I hope this will help you a lot.
Thanks
Java Xml Data StoreJava Xml Data Store I have to do this project and i'm finding it so... be followed up and/or purchased.
You will need to store the
data in a local binary or
XML file. As a good
Java programmer you will use correct Object-Oriented
JAXB Create XML File And Get Data From XML how to create
XML file and how to convert
XML file's
data to
Java Object... an
XML
file using
Java Object then we will read that
XML data as
Java Object..._TO_REPLACE_4
Now the following
Java file demonstrates how to retrieve the
XML data how to store data in XML file - JSP-Servlethow to store
data in
XML file hi
i have to store the
data for example user id and password in a
xml file the input userid and password...,
To solve the problem visit to :
http://www.roseindia.net/servlets/login-
Xml Delete and edit data in xml file using JSPDelete and edit
data in
xml file using JSP I want to know how to delete and edit
data from an
XML file by use of JSP.
I have
XML file having tasks... in the
xml file,I want to delete and edit some tasks using task id then how can i do
xml file creation in javaxml 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 java code - XMLjava code Write a program using SAX that will count the number of occurrences of each element type in
an
XML document and display them. The document
file to be processed should be identified by
the first command-line argument
java code - XMLjava code
i have to store this
file into database i know normal
xml storing ...but how to proceed through nested elements and attributes i dont...;Hi Friend,
Try the following
code:
import javax.xml.parsers.*;
import
Insert XML file data to databaseInsert
XML file data to database
In this tutorial, you will learn how to insert the
xml file data to database
using dom parser.
You all are aware of
XML... to read the
xml file data and save the values
of an
XML file to a Database
Get Data From the XML File Get
Data From the
XML File
Here you will learn to
retrieve
data from
XML file using SAX parser...\comXML>
java EmployeeDetails
Enter
XML file name:Employee-Detail.xml
XML parsing XML file to get java object - XMLparsing
XML file to get
java object Hello,
I'm facing a problem in parsing
XML file to get the
java object.
I've tried to retrieve
data from
XML file using SAX parser.
my
XML file structure is the following
xml code - XMLxml code I Read
xml is used to store and transfer
data can you explain it with sample
code Java get XML File Java get
XML File
... the
XML file.
For this, you need to create a
XML file.
Here is the employee.xml
file:ADS_TO_REPLACE_1
<?
xml version="1.0"?>
<
Getting Data from XML File (Document) from a
XML file. All
xml files store the
data. You can
add and modify the
data... Getting
Data from
XML File (Document)
..._TO_REPLACE_1
This program helps you in retrieving the
data from a
XML
file Sava data from Form to XML file using strutrsSava
data from Form to
XML file using strutrs I'am a biginner with struts want so save
data from my form in an
Xml file using struts but i'm searching witout finding a solution thanks fo your help
Stored Data in XML File using Servlet
Stored
Data in
XML File using Servlet
... to stored
data in
xml file
using Servlet We have created
file login.jsp... a message '
Xml
File Created Successfully'.
JAXP (
Java API for
XML 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 xml file reading using javaxml file reading using java hi deepak
I want to read some
data from
xml file and send that output to particular email address using
java
import org.w3c.dom.*;
import org.w3c.dom.Node;
import javax.xml.parsers.
Display data from xml file to Swings - Swing AWTDisplay
data from
xml file to Swings Hi, We are Preparing a stand alone application. Where the Swings is the front end. There will be only... clicking the buttons in swings it has to display 20 record in one shot in table
how to write java data - XMLhow 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 convert text file to xml file in java. - XMLhow to convert text
file to
xml file in
java. Hi all,
I m having some problem. Problem is I want to convert a text
file which is having the no of record(i.e no of different line of information)to a
xml file through
java Accessing XML file from Java Accessing
XML file from
Java
...
file using
Java
Code.
In this example we have provided you a simple
java example with the source
code that will make it possible to access the
XML file Writing xml file - Java Beginners XmlServlet().createXmlTree(doc);
System.out.println("
Xml File Created...Writing
xml file Thank you for the quick response
The values which...;Hi friend,
Code to solve the problem :
import java.io.*;
import java.sql.
Java code to parse xml - Java BeginnersJava code to parse xml
TL1
10.50.26.98... to parse the
code and get the values from
xml . after fetching the values i have to use them in the same order i mean like
Storing Data (Retrieved from a XML Document) to a File Storing
Data (Retrieved from a
XML Document) to a
File... to store
data
(retrieved from the
XML document) to a specified
file (with ... simple program that helps you in storing
the
data to a specified
file in different
Convert text file to XML file in JavaConvert text
file to
XML file Java
In this section, you will learn how to convert the text
file into
xml file in
Java language. To do this, we have used StreamResult which acts as an holder for a transformation result in
XML. After
Creating XMl file - XMLCreating
XMl file I went on this page: http://www.roseindia.net/
xml/dom/createblankdomdocument.shtml and it shows me how to create an
XML file, however there is something I don't understand. I have to create an
XML file code - XMLcode how to connect mysql with
xml using php
xml file display - XML code to display the above
xml file in tree structure
where...
xml file display
-
-
-
-
ADL SCORM
CAM 1.3
-
-
session3
-
Online Instructional Strategies that Affect Learner
How to delete file in Java code?How to delete
file in
Java code? Hi,
From my
Java program I have to delete a
file.
How to delete
file in
Java code?
Thanks
Hi,
Java... complete example
code at How to delete
file in
Java?.
Thanks
Hi,
Also