my dog learned polymorphism*
The moose likes Servlets and the fly likes create a xml from getting values from html forms Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "create a xml from getting values from html forms " Watch "create a xml from getting values from html forms " New topic
Author

create a xml from getting values from html forms

aru man
Greenhorn

Joined: Aug 04, 2008
Posts: 4
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

Joined: Jan 10, 2002
Posts: 61102
    
  66

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 .


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
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

Joined: Aug 04, 2008
Posts: 4
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

Joined: Aug 04, 2008
Posts: 4
Hi,
William can you just post your way of coming to the solution so that it could be helpful to me......
aru man
Greenhorn

Joined: Aug 04, 2008
Posts: 4
Hi,
William can you just post your way of coming to the solution so that it could be helpful to me......
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

Hi aru man welcome to Javaranch ,
And please UseCodeTags.
Unformatted code is difficult to read so most people might just ignore it.


SCJP, SCWCD.
|Asking Good Questions|
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: create a xml from getting values from html forms