Posted By:
Anonymous
Posted On:
Monday, July 9, 2001 10:25 AM
Hi I created an application that uses the following two methods to handle XML file communication between two servlets on different servers. sendRequest takes two inputs and uses XMLSerializer to send the doc object. recievexml is the listener and retrieves the contents of the inputstream, the rest of the code (held in a package) unpacks the data and stores it in a database, the problem I have is The lines A and B in the sendRequest method cause the following exception: org.xml.sax.SAXParseException: The root element is required in a well-formed document. but the aplication runs o.k, removing the line causes the application to fail, I need help with: 1, Why this situa
More>>
Hi
I created an application that uses the following two methods to handle XML file communication between two servlets on different servers.
sendRequest takes two inputs and uses XMLSerializer to send the doc object.
recievexml is the listener and retrieves the contents of the inputstream,
the rest of the code (held in a package) unpacks the data and stores it in a database, the problem I have is
The lines A and B in the sendRequest method cause the following exception:
org.xml.sax.SAXParseException: The root element is required in a well-formed document.
but the aplication runs o.k,
removing the line causes the application to fail,
I need help with:
1, Why this situation happens (the exception being thrown, some how, completes the communication)
2, And how to solve it.
Thanks
Asante
****************************************
********************************
public static void sendRequest(Document doc, String add) throws
ServletException,
IOException
{
try {
URL url = new URL
("http://" + add);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// inform the connection that we will send output and accept input
conn.setDoInput(true);
conn.setDoOutput(true);
// Don't use a cached version of URL connection
conn.setUseCaches (false);
conn.setDefaultUseCaches (false);
OutputStream out = conn.getOutputStream();
// Specify the content type that we will send binary data
XMLSerializer ser = new XMLSerializer(out, new OutputFormat("xml", "UTF-8", false));
ser.serialize(doc);
out.close();
A - DOMParser parser = new DOMParser();
B- parser.parse(new InputSource(conn.getInputStream()));
} catch (Throwable e) {
e.printStackTrace();
}
}
**********************************************************************
**********************************************************************
public class recievexml extends HttpServlet
{
public void doPost (HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
String mapFilename = "E:/Recieved/SeatRecieved.map",
xmlFilename = "E:/Recieved/RecievedXML.xml",
url = "jdbc:odbc:web_recieved";
PrintWriter out = res.getWriter();
res.setContentType("text/plain");
try
{
//Retrieve the xml file from the input stream
InputStream in = req.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuffer sb = new StringBuffer();
String line = reader.readLine();
while (line != null)
{
sb.append(line);
line = reader.readLine();
}//end while
//Make the file physically available at client end
File outputFile = new File(xmlFilename);
FileWriter outF = new FileWriter(outputFile);
outF.write(sb.toString());
outF.close();
//Store file Contents
MyDBStore.toDBMS(mapFilename,xmlFilename,url);
}//end try
catch (Exception e)
{
out.println(e);
}//end catch
}//end doPost
<<Less