[jdom-interest] parsing XML using JDOM.....Please help!

A. Kevin Baynes kbaynes at seagullsw.com
Mon Jul 7 07:39:28 PDT 2003


Looks like you are printing back to the browser, and your code looks good up
to here :
xout.output(studentRegistrationdoc, out);

I'm guessing that either you aren't checking for the response back to the
browser, or that it's not getting sent because you aren't flushing the
OutputStream. You should do :
out.flush();
out.close();
at the end of doGet();

The line : 'System.out.println(xout);' is trying to print the XMLOutputter
object to stdout... that isn't going to work. You want the contents of the
Document written to stdout, try replacing that line with
'xout.output(studentRegistrationdoc, System.out);'.

~akb

-----Original Message-----
From: jdom-interest-admin at jdom.org [mailto:jdom-interest-admin at jdom.org]On
Behalf Of Naveen My
Sent: Monday, July 07, 2003 9:31 AM
To: jdom-interest at jdom.org
Subject: [jdom-interest] parsing XML using JDOM.....Please help!


Hello ,
I am using below code to get data from .html FORM and then buid XML.
Presently,I am writing the XML I generated to the browser.

I would like to parse the XML thatw as generated and get the values out to
insert in the database using JDBC.I know how to use
JDBC(connectin,statement,execution ....)

All I am looking for is to parse the XML doc I create and get the values out
...so that I can insert the values in the database....

Your help is highly appreciated....
thanks
naveen


import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;

public class XML3 extends HttpServlet
{
    public void doGet(HttpServletRequest request,
                      HttpServletResponse response)
        throws IOException, ServletException
    {
        response.setContentType("text/xml");
        PrintWriter out = response.getWriter();
        try
        {
            Element studentRegistration = new
Element("studentRegistration");

            Element studentID = new Element("studentID");
            studentID.addContent(request.getParameter("studentID"));
            studentRegistration.addContent(studentID);

            Element firstName = new Element("firstName");
            firstName.addContent(request.getParameter("firstName"));
            studentRegistration.addContent(firstName);

            Element lastName = new Element("lastName");
            lastName.addContent(request.getParameter("lastName"));
            studentRegistration.addContent(lastName);

            Element ssn = new Element("ssn");
            ssn.addContent(request.getParameter("ssn"));
            studentRegistration.addContent(ssn);

            Element address = new Element("address");
            address.addContent(request.getParameter("address"));
            studentRegistration.addContent(address);

            Element city = new Element("city");
            city.addContent(request.getParameter("city"));
            studentRegistration.addContent(city);

            Element state = new Element("state");
            state.addContent(request.getParameter("state"));
            studentRegistration.addContent(state);

            Element zip = new Element("zip");
            zip.addContent(request.getParameter("zip"));
            studentRegistration.addContent(zip);

            Element emailAddress = new Element("emailAddress");
            emailAddress.addContent(request.getParameter("emailAddress"));
            studentRegistration.addContent(emailAddress);

            Element userID = new Element("userID");
            userID.addContent(request.getParameter("userID"));
            studentRegistration.addContent(userID);

            Element password = new Element("password");
            password.addContent(request.getParameter("password"));
            studentRegistration.addContent(password);

            Document studentRegistrationdoc = new
Document(studentRegistration);

            XMLOutputter xout = new XMLOutputter(" ", true);
            xout.output(studentRegistrationdoc, out);
            System.out.println(xout);
       }
       catch (Exception e)
       {
            e.printStackTrace();
       }
    }
    public void doPost(HttpServletRequest request,
                      HttpServletResponse response)
        throws IOException, ServletException
    {
        doGet(request, response);
    }
}



Do you Yahoo!?
SBC Yahoo! DSL - Now only $29.95 per month!




More information about the jdom-interest mailing list