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



Home Answers Viewqa JSP-Servlet 17124-Java-code-to-append Java code to append/add data into a existing xml file

Have Programming Question? Ask it here!
 
 
 


gurpreet singh
Java code to append/add data into a existing xml file
3 Answer(s)      3 years and 2 months ago
Posted in : JSP-Servlet


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 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <input type="text" name="text1"/>
      </P>
      <P>Address &nbsp;&nbsp;
      <input type="text" name="text2"/></P>
      <P>Contact&nbsp;&nbsp;&nbsp;&nbsp;
      <input type="text" name="text3"/></P>
      <P>Email &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      <input type="text" name="text4"/></P>
      <P> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
      </P>
      <P> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <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


Advertisement
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 file
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
Insert element into an existing xml file - Java Interview Questions
Insert element into an existing xml file  Dear all, how can i insert elements into an existing xml file? now when i am going to insert new elements... the example code at http://www.roseindia.net/xml/dom/xml-tutorial.shtml Post your
Advertisements
Insert element into an existing xml file - Java Interview Questions
Insert element into an existing xml file  thanks for the reply , I... to be set inside the existing root node, thanks public void writeToFile() throws... = "firstName"; final String lName = "lastName"; FileWriter file = new
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
parsing xml file using java code
parsing xml file using java code  parsing a xml file using java code
How to append text to an existing file in Java
How to append text to an existing file in Java  How to append text to an existing file in Java
data retrivel code - XML
data 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
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
java program to create xml file with append data in well-formed
java program to create xml file with append data in well-formed   Sorry sir your given xml append data program is not well-formed. I'll make you more clear what I want. If this is my xml file Tom Cruise 45 Now when I append
i want java code for this xml file...please show me..
i want java code for this xml file...please show me..   xbrli:shares xbrli:pure iso4217:INR
Java Xml Data Store
Java 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
Write a program in Java to append content at the end of an already existing file.
Write a program in Java to append content at the end of an already existing file.  Write a program in Java to append content at the end of an already existing file
JAXB Create XML File And Get Data From XML
JAXB Create XML File And Get Data From XML In this section we will read about how to create XML file and how to convert XML file's data to Java Object... the Java code and JAXB codes has made easy to work with XML files. We can use
data insertion from xml file to database table
data insertion from xml file to database table  Hi all, I have data in the XML file. I need to insert it into table in the database using servlet. so please reply me . ThankYou
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
java code - XML
java 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
Delete and edit data in xml file using JSP
Delete 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
java code - XML
java 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
how to store data in XML file - JSP-Servlet
how 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
Create XML file from flat file and data insert into database
Create XML file from flat file and data insert into database... have developed an application to create xml file from flat file and data...). Create a file "FlatFileXml.java"  used to create an XML and data
Insert XML file data to database
Insert 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... of program: In this example you need a well-formed XML file that has some data (Emp
Produces XML file but format not correct for storing data using JSP and XML
Produces XML file but format not correct for storing data using JSP and XML  hii I have created a project using JSP and XML as database to store data entered by user in XML file ,It stores data entered in XML file
parsing XML file to get java object - XML
parsing 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
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
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: <?xml version="1.0"?> <company>
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
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)   ...: This program helps you in retrieving the data from a XML file. It takes a xml file
xml file reading using java
xml 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.
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 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
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
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.
Sava data from Form to XML file using strutrs
Sava 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
Data needs to be gathered in XML file from the database (MySql) using JSP
Data needs to be gathered in XML file from the database (MySql) using JSP ... data regarding particular id from the database table. Data needs to be gathered in XML file from the database (MySql) using appropriate JSP/Java Bean functions
Accessing XML file from Java
) AccessingXmlFile.java Source Code for accessing XML file through Java  ... Accessing XML file from Java   ... file using Java Code. In this example we have provided you a simple java
Display data from xml file to Swings - Swing AWT
Display 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 append data to XML file in proper format using JSP
how to append data to XML file in proper format using JSP  hello i was appending my XML file to add more data entered by user in JSP page.But...;/html> XMl file creates successfully at first instance but unable to append
Java code to parse xml - Java Beginners
Java 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
Append a string to an existing file
Append a string to an existing file In this section, you will learn how to append a string to existing file. This will be done using FileWriter... constructor : public FileWriter(File file, boolean append) throws IOException
xml code - XML
xml code  I Read xml is used to store and transfer data can you explain it with sample code
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 ...;java StoreData Enter XML file name: Employee-Detail.xml File
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 store data entered by User in JSP page in XML file
  JSP store data entered by user into XML file 1)form.jsp: <html>...How to store data entered by User in JSP page in XML file  How to store data entered by user in JSP page to be saved in XML file.On clicking submit
Convert text file to XML file in Java
Convert 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
java program for writing xml file - Java Beginners
java program for writing xml file  Good morning i want to write values from my database(one table)into one xml file. Like i have 3 coloumns in my... xml file and storet that in particlar location. Please help me out Thanks
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 store data entered in JSP page by a user in XML file & later retrieval of data using id at other page
How to store data entered in JSP page by a user in XML file & later retrieval... file.How can i store data entered by user in XML file and later retrieve data... .I am using Jdeveloper..xml file creates successfully,but i want data entered
Java code to convert pdf file to word file
Java code to convert pdf file to word file  How to convert pdf file to word file using Java
storing data in xml - XML
storing data in xml  Can u plz help me how to store data in xml using java  Hi Friend, Try the following code: import java.io....,name,address,contactNo,email); System.out.println("Xml File Created
Hibernate Data Filter using XML
In this section, you will learn to filter data using XML mapping file
 

 

 

DMCA.com