Originally posted by Michael Ernest:
What version of Xerces are you using?
I figured out the problem. It seems the Xerces parser 1.4.3 I used may not support in parsing the document out of a ServletInputStream.
I tried the Xerces 2.0. But it seems it conflicted with my other codes and cause other problems.
So I tried another way by creating a inputsource object from a
String containning the XML content. And it worked.
The reason Xerces parser complained is the XML file servlet received contains some content header and form name value .... stuff like that. That's the reason Xerces parser 1.4.3 can't recognize the XML file.
I did it as follows:
String xmlString ="";
BufferedReader br = new BufferedReader( new InputStreamReader ( req.getInputStream(),"ISO-8859-1" ), 1024);
for (int i=0; (line=br.readLine())!=null;i++) {
xmlString += line;
// only grab the XML content and remove others.
index = xmlString.indexOf("<?");
lastIndex = xmlString.lastIndexOf(">");
xmlString = xmlString.substring(index,lastIndex+1);
DOMParser ps = new DOMParser();
InputSource is = new InputSource (new StringReader (xmlString));
ps.parse(is);
Another thing is when uploading the file, the form need to have ENCTYPE="multipart/form-data" as one attribute. It will cause other <input> name value in the form invalid. It seems server won't received other name value pairs using request.getParameter("..").
So if you want to forward some parameter to server side, you need to append the name value pair at the action part like the following:
<form ENCTYPE="multipart/form-data" ACTION="/SomeServlet?name=<%= value %>" METHOD="POST">
I still don't think I did it efficiently, If Michael or anyone have experience in handling the upload XML files, which parse can do the work, etc, please reply, thanks!