Results 1 to 9 of 9
  1. #1
    Join Date
    Mar 2011
    Posts
    35
    Rep Power
    0

    Default how to append data entered by user 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.please 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.stre am.*,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>

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,861
    Rep Power
    19

    Default

    First off, can you use code tags please?

    Next you'll need to give us some more information, like what problem do you have? What are you seeing? What should you be seeing? Any errors etc?

    And finally, that mass of code is frankly an abuse of a JSP. You should probably be building your XML in a servlet then forwarding that to your JSP page. That, at least, woudl be easier to debug than this mess.

  3. #3
    Join Date
    Mar 2011
    Posts
    35
    Rep Power
    0

    Default plz help

    Demo of JSP Web Application demonstrating web technology concepts?
    Thanx for reply,sry new here no idea about code tags.My assinment::
    Create an application (Task manager)for users to create new task ,edit and delete task using JSP and XML(XML to store data).User can see status (pending,done) of the task.Definition of task is as follows:

    Task_id(Auto generated)
    Task_name
    Taske_status
    Project
    Comments
    Creation Date

    Now the code i have provided creates XML file but that is not in proper format(as xml rules),I am unable to understand where i mistaken,Please help me to complete that assignment,Ill be highly thankful,(This is not the assignment but simple code to understand how to make XML and store data in that). I am using jdeveloper and it server for the purpose.others not available.

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,861
    Rep Power
    19

    Default

    You really haven't answered my key questions.
    What do you see?
    What should you see?

    And code tags look like [ code ] [ /code ] without the spaces.

    And it's still an abuse of JSPs.
    Whoever is teaching you this should have started with servlets then moved onto JSPs, so you don't end up sticking gtreat slabs of code into a JSP page.

  5. #5
    Join Date
    Mar 2011
    Posts
    35
    Rep Power
    0

    Default what i see and wat shud it be?

    what i see?
    XML file creates successfully but not in proper format
    [code]
    <?xml version = '1.0' encoding = 'UTF-8'?>
    <Stu>
    <Student>
    <Name>111</Name>
    <Address>222</Address>
    <ContactNo>333</ContactNo>
    <Email>444</Email>
    </Student>
    </Stu><?xml version = '1.0' encoding = 'UTF-8'?>
    <Stu>
    <Student>
    <Name>11</Name>
    <Address>111</Address>
    <ContactNo>1</ContactNo>
    <Email>11</Email>
    </Student>
    </Stu><?xml version = '1.0' encoding = 'UTF-8'?>
    <Stu>
    <Student>
    <Name>55</Name>
    <Address>ww</Address>
    <ContactNo>ww</ContactNo>
    <Email>1232</Email>
    </Student>
    </Stu><?xml version = '1.0' encoding = 'UTF-8'?>
    <Tasks>
    <task>
    <Taskid>1</Taskid>
    <Taskname>pqr</Taskname>
    <Project>pqr1</Project>
    <Date>9 march</Date>
    </task>
    </Tasks>
    [code]
    what shud i see?
    proper XML format code as per rules
    [code]
    <?xml version = '1.0' encoding = 'UTF-8'?>
    <Stu>
    <Student>
    <Name>111</Name>
    <Address>222</Address>
    <ContactNo>333</ContactNo>
    <Email>444</Email>
    </Student>
    <Student>
    <Name>112</Name>
    <Address>22</Address>
    <ContactNo>3</ContactNo>
    <Email>44</Email>
    </Student>
    <Student>
    <Name>111</Name>
    <Address>222</Address>
    <ContactNo>333</ContactNo>
    <Email>444</Email>
    </Student>
    </Stu>
    [code]

  6. #6
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,861
    Rep Power
    19

    Default

    Could you format your code please?
    How many times do I have to mention this?

    Anyway:
    Java Code:
    FileWriter fw=new FileWriter(file,true);
    You're appending the document each time to the file, which is why you're getting the xml header info.

    Not sure why the entires are going in separately, but I can't follow your code at the moment.

  7. #7
    Join Date
    Mar 2011
    Posts
    35
    Rep Power
    0

    Default

    Thanx but I am already using this command,it edit the XML file but adds data at last but also adds header line of XML file and root node also again and again which corrupts as rule specified by XML,only one root node should be there,bt my code adding root node also again and again.plz help yaar...thnx

  8. #8
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,861
    Rep Power
    19

    Default

    Yes.
    It's doing that because you are appending!

    It's just a text file.
    It doesn't know it isn't supposed to stick the xml header on there.

    You've just told the FileWriter to append the contents of the Doc to the file. Which is what it's doing. The contents of the Doc include the xml header!

  9. #9
    Join Date
    Mar 2011
    Posts
    35
    Rep Power
    0

    Default

    So sir Plz help me with the code,to modify in such a way tha it do not appends doc and root node again and again to the file,Thank you

Similar Threads

  1. Replies: 5
    Last Post: 11-22-2010, 07:33 PM
  2. How to append data to an already existing file?
    By siteregsam in forum New To Java
    Replies: 3
    Last Post: 05-03-2010, 08:06 PM
  3. How to Append in file ?
    By Hippo in forum New To Java
    Replies: 2
    Last Post: 03-19-2010, 01:50 PM
  4. Boggle game - verify words entered by user
    By dragonwolf in forum New To Java
    Replies: 11
    Last Post: 11-30-2009, 04:18 AM
  5. Replies: 2
    Last Post: 04-07-2009, 06:25 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •