Win a copy of Mastering Corda: Blockchain for Java Developers this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

help! How to parse the XML file uploaded from clients?

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am doing a project requiring me to parse the XML file uploaded from client side.
In client side, the uploading page contains:
<form ENCTYPE="multipart/form-data" ACTION="XMLUploadServlet" METHOD="POST">
...
</form>
In server side, a Servlet handles the request. In doPost method of this servlet:

Xerces parser is usded.
After I ran the program, I got the following exception:
org.xml.sax.SAXParseException: The root element is required in a well-formed document.

But the XML file is valid against the DTD along with it.
Any one has the similar experience?
Any suggestion is appreciated.
Thanks in advance!
 
High Plains Drifter
Posts: 7289
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What version of Xerces are you using?
 
Brooklyn Luo
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!
 
What? What, what, what? What what tiny ad:
the value of filler advertising in 2020
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic