Results 1 to 9 of 9
- 03-07-2011, 11:45 AM #1
Member
- Join Date
- Mar 2011
- Posts
- 35
- Rep Power
- 0
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>
- 03-07-2011, 02:09 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.
- 03-08-2011, 04:37 AM #3
Member
- Join Date
- Mar 2011
- Posts
- 35
- Rep Power
- 0
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.
- 03-08-2011, 08:21 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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.
- 03-08-2011, 09:12 AM #5
Member
- Join Date
- Mar 2011
- Posts
- 35
- Rep Power
- 0
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]
- 03-08-2011, 09:28 AM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Could you format your code please?
How many times do I have to mention this?
Anyway:
You're appending the document each time to the file, which is why you're getting the xml header info.Java Code:FileWriter fw=new FileWriter(file,true);
Not sure why the entires are going in separately, but I can't follow your code at the moment.
- 03-09-2011, 06:38 AM #7
Member
- Join Date
- Mar 2011
- Posts
- 35
- Rep Power
- 0
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
- 03-09-2011, 08:42 AM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
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!
- 03-09-2011, 11:22 AM #9
Member
- Join Date
- Mar 2011
- Posts
- 35
- Rep Power
- 0
Similar Threads
-
How to obtain MAX/MIN Values through user data entered?
By apalacpac in forum New To JavaReplies: 5Last Post: 11-22-2010, 07:33 PM -
How to append data to an already existing file?
By siteregsam in forum New To JavaReplies: 3Last Post: 05-03-2010, 08:06 PM -
How to Append in file ?
By Hippo in forum New To JavaReplies: 2Last Post: 03-19-2010, 01:50 PM -
Boggle game - verify words entered by user
By dragonwolf in forum New To JavaReplies: 11Last Post: 11-30-2009, 04:18 AM -
File fp = new File(filePath);fp.exists() does not yeild proper result
By ganeshp in forum Advanced JavaReplies: 2Last Post: 04-07-2009, 06:25 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks