• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

create a xml from getting values from html forms

 
aru man
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
using servlets how to create a xml from getting the values for the xml from html forms...
1)i hav created a html form ..how can i convert the values to a xml form...
[ August 05, 2008: Message edited by: aru man ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please be sure to take the time to compose descriptive subjects for your posts; read this for more information.

Using a title of "servlets" in a forum completely dedicated to questions on Servlets isn't very helpful.

You can go back and change your post to add a more meaningful subject by clicking the .
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13073
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The simplest way to create an XML document given values that you have obtained from an HTML request in the usual way is to just write plain text.

Where does the resulting XML have to go? Using standard java you can write to a file or a String in memory or a variety of other output streams.

Do you have a sample of what the XML is supposed to look like?

Bill
 
aru man
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,
i have generated the xml from html form....
the code is as below..


import java.io.*;
import javax.servlet.Servlet;
import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import org.w3c.dom.*;


/**
* @version 1.0
* @author
*/
public class servxml extends HttpServlet implements Servlet {

/**
* @see javax.servlet.http.HttpServlet#void (javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
resp.setContentType("text/html");
//BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = resp.getWriter();
//ring root = req.getParameter("root");
String c="class1";
String c1="method";
String c2="param";


String s = req.getParameter("class1");
String s1 = req.getParameter("method");
String s2 = req.getParameter("param");
try{

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document =db.newDocument();
String root = req.getParameter("root");
Element rootElement = document.createElement(root);
document.appendChild(rootElement);

//Element em = document.createElement(class);
Element em = document.createElement(c);
em.appendChild(document.createTextNode(s));
rootElement.appendChild(em);


Element er = document.createElement(c1);
er.appendChild(document.createTextNode(s1));
rootElement.appendChild(er);

Element et= document.createElement(c2);
et.appendChild(document.createTextNode(s2));
rootElement.appendChild(et);


TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(System.out);
transformer.transform(source, result);
out.println("<html><body>");
out.println("<h3>xml created");
out.println("</body></html>");

}
catch(Exception e){e.printStackTrace();
}


}
}


but i am getting the result in console...
1)
how to display it in next window..
 
aru man
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
William can you just post your way of coming to the solution so that it could be helpful to me......
 
aru man
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
William can you just post your way of coming to the solution so that it could be helpful to me......
 
Amit Ghorpade
Bartender
Posts: 2854
10
Fedora Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi aru man welcome to Javaranch ,
And please UseCodeTags.
Unformatted code is difficult to read so most people might just ignore it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic