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 not getting XML file in proper format as XML rules.lease help me to get XML file in proper format.I am using Jdeveloper. 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(String name,String address,String contact,String email) throws Exception {
        Element root;
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = builderFactory.newDocumentBuilder();
        Document doc = docBuilder.newDocument();
        File file = new File("c:/new.xml");
        if (file.exists())
        {
          //DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
          //DocumentBuilder builder = fact.newDocumentBuilder();
          doc = docBuilder.parse(file);
          root = doc.getDocumentElement();
          String sr = root.getNodeName();


          //root = node.getNodeName();
        }
        else
        {

          System.out.println(name);
          root = doc.createElement("Students");
          doc.appendChild(root);
        }

        Element child = doc.createElement("Student");
        root.appendChild(child);

        Element child1 = doc.createElement("Name");
        child.appendChild(child1);

        Text text1 = doc.createTextNode(name);
        child1.appendChild(text1);

        Element child2 = doc.createElement("Address");
        child.appendChild(child2);

        Text text2 = doc.createTextNode(address);
        child2.appendChild(text2);

        Element child3 = doc.createElement("ContactNo");
        child.appendChild(child3);

        Text text3 = doc.createTextNode(contact);
        child3.appendChild(text3);

        Element child4 = doc.createElement("Email");
        child.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();
        FileWriter fw=new FileWriter(file,true);
        BufferedWriter bw = new BufferedWriter(fw);
        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(name,address,contact,email);


    out.println("<b>Xml File Created Successfully</b>");
  }
  catch(Exception e)
  {
    System.out.println(e);
  }

  %>
  </body>
</html>

XMl file creates successfully at first instance but unable to append data entered bt more users in proper XML format. I have also tried following code but that also not in proper format: please provide proper code for forming proper XML file:

<%@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(String name,String address,String contact,String email) throws Exception {
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = builderFactory.newDocumentBuilder();
        Document doc = docBuilder.newDocument();
        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:/emp.xml");
        FileWriter fw=new FileWriter(file,true);
        BufferedWriter bw = new BufferedWriter(fw);
        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(name,address,contact,email);


    out.println("<b>Xml File Created Successfully</b>");
  }
  catch(Exception e)
  {
    System.out.println(e);
  }

  %>
  </body>
</html>
View Answers

March 9, 2011 at 4:51 PM

The code is working .. it append the record with the root signature of xml that's "". i dont want with every record.. Kindly help me in this.. i tried a lot. Thank you


May 18, 2011 at 4:34 PM

Thanks, it is very good









Related Tutorials/Questions & Answers:
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 not getting XML file in proper format as XML rules.lease help me to get XML file
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... XML file also(wrong format),shows result in JSP page but server shows
Advertisements
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
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... .I am using Jdeveloper..xml file creates successfully,but i want data entered by user to be stored in xml file.here you have entered data in JSP file
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 will be coming from jsp middle ware servlet how to do that?   Hi friend
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
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
HOW TO STORE MULTIPLE EMPLOYEE DETAILS TO XML FILE USING JSP?
HOW TO STORE MULTIPLE EMPLOYEE DETAILS TO XML FILE USING JSP?  HELLO SIR, CAN ANYONE HELP ME OUT HOW TO STORE MULTIPLE EMPLOYEE DETAILS TO XML... HELP ME OUT WITH HOW TO VIEW THE STORED DATA IN XML FILE USING EMPNAME. PLEASE
Java code to append/add data into a existing xml file
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...; </html> This JSP code overwrites XML file with new data but not appends
database data in xml format
database data in xml format  HI, i want to display the database data in the xml format(not as xml file ) on the console using DOM. help will be appreciated. THANKS K.K
How to store data entered by User in JSP page in XML file
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...   JSP store data entered by user into XML file 1)form.jsp: <html>
how to create an xml file in following clear format
how to create an xml file in following clear format  anyone please help me out to create this file,,. <Tasks> <Taskid>...;Taskid>3 <Taskname>Integration <Project>Assinment JSP
how to save html form data into .csv file using only jsp.
how to save html form data into .csv file using only jsp.  Dear all, I am developing a website only using jsp and html. i need to save the form data into a .csv file using jsp. can anyone give me any sample solution or tutorial
Java append new node to XML file
Java append new node to XML file In this tutorial, you will learn how to append new node to xml file. Sometimes there is a need to append a new XML node to the xml file. Here is an xml file where we are going to append a new node
How to convert excel file int xml format in java
How to convert excel file int xml format in java  How to read excel file(xls) which is stored in any directory(D://) and convert it into xml format in java and place in any directory(E://).Can any body provide the code
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... in xml file. It creates XML file with its version and encoding and display
sorting and filtering the displayed table data using jsp and xml
sorting and filtering the displayed table data using jsp and xml  I have created a xml file and a jsp file, which uses DOM parser to display the content of xml file in web page. Now I would like to sort and filter those data
Read data from excel file and update database using jsp
Read data from excel file and update database using jsp  read data from excel file and update database using jsp Hi, I am using a MySQL database... format and I need to update MySQL tables with new experimental data. How can I
How to upload file using JSP?
How to upload file using JSP?   Hi all, I m the beginner in JSP, I want to upload file on server in specific folder.   1)page.jsp...;% // for uploading the file we used Encrypt type of multipart/ form-data and input of file type
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 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
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
How to generate xml file using xpath
How to generate xml file using xpath  Hi, I have a requirement... to make a xml using that,. It would be great is you can help me out with how to make a xml using xpath. Thanks in Advance, SanjayADS_TO_REPLACE_1   Hi
how to write append file in Java
how to write append file in Java  How to write append file in Java   Hi, For append in the test new file or Appending a text earlier...("appenToFile.txt", true); For More Details visit this link: How to Append file
how to read data from excel file through browse and insert into oracle database using jsp or oracle???
how to read data from excel file through browse and insert into oracle database using jsp or oracle???  sir.. i have number of excel sheets which...://www.roseindia.net/answers/viewqa/JSP-Servlet/28123-write-excel-file-into-the-oracle
parsing xml file in jsp
parsing xml file in jsp  example that pars XML file in JSP
how to search data in xml files using php
how to search data in xml files using php  So I want to create..., and have it search multiple XML pages on server The XML documents are setup and available on the server but I want to access it through php. How can I do
Hibernate Data Filter using XML
In this section, you will learn to filter data using XML mapping file
Reading a xml file - JSP-Servlet
Reading a xml file  how to read a xml file using jsp and then i have to retrive a data from that file use it in code?  Hi Friend, Please visit the following link: http://www.roseindia.net/jsp/parsing-xml.shtml
How to read and display data from a .properties file from a jsp page
How to read and display data from a .properties file from a jsp page  I have a .properties file. I have to create a jsp page such that it reads the data from this .properties file and display it in table format. Ex:by using
Set Data Format in Excel Using POI 3.0
Set Data Format in Excel Using POI 3.0   ... various file formats based upon Microsoft's OLE 2 Compound Document format using... of this will be 11,111.1 .To set the data format we are using
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
How to get xml file form http port using web service
How to get xml file form http port using web service  hi I am suresh... and use the xml data to convert it to java file and store it to DB. But i am struck with getting xml file. How to access xml file in web service? Please Help
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
How I Upload File and Store that file name in Database using JSP
How I Upload File and Store that file name in Database using JSP  Hi All, I m the beginner in JSP and I want to upload the file and store that file and some other form data in MySQL database. Ex. There is one employee detail
how to insert data into database using jsp & retrive
how to insert data into database using jsp & retrive  Hello, I have created 1 html page which contain username, password & submit button. in my oracle10G database already contain table name admin which has name, password
Java append file
Java append file In this section, you will learn how to append the text... to the existing file by using either FileOutputStream or by FileWriter class. The data... if you put append value to true with the file name then the specified data
How to browse excel file and stored the contents into the database using jsp/servlet?
How to browse excel file and stored the contents into the database using jsp/servlet?  Hi.. I want to browse excel file and stored the file data into the My-sql database using jsp/servlet
how to insert data in database using html+jsp
how to insert data in database using html+jsp  anyone know what... of connection url within specified format with machine name, port number..."; // declare a connection by using Connection interface
How to retrieve data by using combo box value in jsp? - JSP-Servlet
How to retrieve data by using combo box value in jsp?  I am using a single jsp form.. i did not get from another form.. i do not use 2 jsp form.. all.... Note: I am using only this jsp form.. i do not use
how to display data from jsp file into database
how to display data from jsp file into database  this is a jsp file...+","+ph2+")"); out.println("Data is successfully inserted...+",'"+email+"')"); out.println("Data is successfully inserted into database
how to make a proper arrangement of a string - JSP-Servlet
how to make a proper arrangement of a string  Dear sir, I... .Thanks Hr. so now i want to send a matter with a proper manner i.e following... is 2.0 and el is 10.0 . Thanks Hr. so how to do this sir please help me
How to retrieve data by using combo box value in jsp? - JSP-Servlet
How to retrieve data by using combo box value in jsp?  For example, In Employee.jsp form, When i click employee id value in combo box ,the related employee name will be displayed in text field
How to retrieve data using combo box value in jsp? - JSP-Servlet
How to retrieve data using combo box value in jsp?  Hi freind, I already post this question. I need urgent help from u. pl response me... the following link: http://www.roseindia.net/jsp/comboSelect.shtml Hope
How to export data from jsp to excel sheet by using java
How to export data from jsp to excel sheet by using java   How to export data from jsp to excel sheet by using java
How to download files from server to local disk using ZIP format in JSP page - JSP-Servlet
How to download files from server to local disk using ZIP format in JSP... disk creting ZIP format for all files .how can create ZIp for all files pls help me as did like this but when i click on file it shows some depricted form and also
To save table format data in pdf/excel in jsp
To save table format data in pdf/excel in jsp  Hello, I am doing web application project in jsp. In webform ,I am displaying database data in html table. So my question is ,I want so save this html format data in pdf/excel format
Date not getting in proper format
Date not getting in proper format  import java.util.*; class Date { public static void main(String[]args) { Date d = new Date(100000000000L); System.out.print("1st date"+d.toString()); } } out put:- 1st date@3e25
How to append a request parameter in a jsp page - JSP-Servlet
How to append a request parameter in a jsp page   i have a jsp page... page have same item which i will use same for all school. i want to know how... access navigate the data based on school name
How i upload file and save that record in database using JSP?
How i upload file and save that record in database using JSP?  Hi All, I m the beginner in JSP and I want to upload the file and store that file and some other form data in MySQL database. Ex. There is one employee detail form

Ads