You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-dev@xerces.apache.org by "Jian, David" <DJ...@priceinteractive.com> on 2001/02/15 19:48:23 UTC

SAXParseException from parser.pase()

Hi,

I am using xml on http sample code with xerces-123 and I get I get
"SAXParseException: The root element is required in a well-formed document."
on the parser.parse(...) line. Yes, the XML is well-formed.  I also try to
use a well-formated xml file, but that exception is still there.



import org.w3c.dom.*; 
import org.apache.xerces.*; 
import org.apache.xerces.dom.*; 
import org.apache.xerces.parsers.*; 
import org.apache.xml.serialize.*; 
import org.xml.sax.*; 
import java.net.*; 
import java.io.*; 

class XmlBridge {   
    private String sURI; 
    public XmlBridge(String serverURI) { 	
	sURI = serverURI; 
    } 
    public Document sendRequest(Document doc) { 
	Document docOut = null; 	
	try { 
	    URL url = new URL("http://" + sURI); 
	    HttpURLConnection conn = 
	               (HttpURLConnection) 
	                   url.openConnection(); 
	    conn.setDoInput(true); 
	    conn.setDoOutput(true); 
	    OutputStream out = 
	                 conn.getOutputStream(); 
	    XMLSerializer ser = 
	    	new XMLSerializer(out, 
	    	         new OutputFormat("xml", 
	    	                "UTF-8", false)); 
	    ser.serialize(doc); 
	    out.close(); 
	    DOMParser parser = new DOMParser(); 
	    parser.parse(new 
	      InputSource(conn.getInputStream())); 
	    docOut = parser.getDocument(); 
	} catch (Throwable e) { 
	    e.printStackTrace(); 
	} 
	return docOut; 
    }     
    public static void main(String[] args) { 
	try {  // Build up an XML Document 
	    Document docIn = new DocumentImpl(); 
	    Element e = 
	           docIn.createElement("Order"); 
	    e.appendChild(docIn.createElement(
	                               "Type")); 
	    e.appendChild(docIn.createElement(
	                             "Amount")); 
	    docIn.appendChild(e); 
	    // Send it to the Servlet 
	    XmlBridge a = 
	                new XmlBridge(args[0]); 
	    Document docOut = 
	                   a.sendRequest(docIn); 
	    // Debug - write the sent 
	    //Document to stdout 
	    XMLSerializer ser = 
	           new XMLSerializer(System.out, 
	                                   null); 
	    ser.serialize(docOut); 
	} catch (Throwable e) { 
	    e.printStackTrace(); 
	} 
    } 
} 
--------------------- 
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*; 
import org.w3c.dom.*;
import org.xml.sax.*; 
import org.apache.xerces.*; 
import org.apache.xerces.dom.*; 
import org.apache.xerces.parsers.*;
import org.apache.xml.serialize.*; 

public abstract class XmlServlet 
                 extends GenericServlet {     
    protected abstract Document 
                 doRequest(Document req);
    public XmlServlet() {} 
    public void service(ServletRequest 
               req, ServletResponse res)  
                 throws ServletException { 
	try {
	    DOMParser parser = 
	              new DOMParser();
	    //problem here
	    parser.parse(new InputSource(
	           req.getInputStream())); 
	    Document docOut = 
	             doRequest(
	             parser.getDocument()); 
	    XMLSerializer ser = 
	    	new XMLSerializer(
	    	     res.getOutputStream(), 
	    	         new OutputFormat("xml",
	    	              "UTF-8", false));
	    ser.serialize(docOut);	    
	    res.getOutputStream().close(); 
	} catch (Throwable e) {	    
	    e.printStackTrace(); 
	} 
    } 
} 

 I use VCafe to bebug the xmlservlet. For some reason in line
parser.parse(new InputSource(req.getInputStream())), parse() method is
frozen and stop running. Is anyone has some solution or experience on this
problem. Any help will be appreciated.

DJ