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



Home Answers Viewqa JSP-Servlet how to append data to XML file in proper format using JSP
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


gurpreet singh
how to append data to XML file in proper format using JSP
2 Answer(s)      3 years and 9 months ago
Posted in : JSP-Servlet


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>

Advertisement
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
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
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 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 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
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
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
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
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 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>
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
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
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
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
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 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
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
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, Sanjay   Hi Friend, Please
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
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
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
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...;} } Through the above code, you can append data to the text file
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
Hibernate Data Filter using XML
In this section, you will learn to filter data using XML mapping file
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
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 setDataFormat() method
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
 

 

 

DMCA.com