Hi,
 
I'am facing following problem:
 
In a servlet I want to read from a stream a xml document (the user defined in a form a local xml document with the 'browse' button in a 'file' input fiels <input type="file">).
 
The xmlstream in the code is defined as xmlstream = filePart.getInputStream (filePart is an instance of the  filePart class in the com.oreilly.servlet.* package)
.
The corresponding xsl document is located on the server. the XSL systemid is correctly set.
 
I always get following message : The root element is required in a well-formed document.
 
This seems to me that the SAXParser does not automatically reads the bytes from the xmlstream.
I do have the impression that the SaxParser thinks the stream is empty !?
 
I understood from the API doc that the streal will automatically be read by Xalan.
 
Can anyone give me a hint ?
 
Please find below an excerpt of the code:
 
Thx,
 
 Stefan
 
 
  ...
 
  public void transform()
   throws TransformerException, TransformerConfigurationException,
            IOException, SAXException
 {
   String media= null , title = null, charset = null;
 
   try
   {
          Transformer transformer ;
          PDXxml2pdf handler = new PDXxml2pdf();
          XMLReader reader ;
          SAXSource saxsource ;
          Source    xmlsource;
          Source    xslsource ;
         
 
          // Define xmlsource
          if (xmlstream != null)
              xmlsource = new StreamSource(xmlstream); // xmlstream is a defined inputstream
 
          // Since we're going to use a SAX feature, the transformer must support
          // input in the form of a SAXSource.
          TransformerFactory tfactory = TransformerFactory.newInstance();
        
          if(tfactory.getFeature(SAXSource.FEATURE)) {
            System.out.println("Activating DTD validation...");
 
            // Standard way of creating an XMLReader in JAXP 1.1.
            SAXParserFactory pfactory= SAXParserFactory.newInstance();
 
            // Turn on support for XML namespaces.
            pfactory.setNamespaceAware(true);
 
            // Turn on validation.
            pfactory.setValidating(true);
 
            // Get an XMLReader.
            reader = pfactory.newSAXParser().getXMLReader();
 
            // Instantiate an error handler (see the Handler inner class below) that will report any
            // errors or warnings that occur as the XMLReader is parsing the XML input.
            reader.setErrorHandler(handler);
 
            // Specify a SAXSource that takes both an XMLReader and a URL.
            saxsource = new SAXSource(reader, SAXSource.sourceToInputSource(xmlsource));
 
         } else {
           System.out.println("tfactory does not support SAX features! No DTD validation will be done");
           saxsource = new SAXSource(SAXSource.sourceToInputSource(xmlsource));
         }
 
 
         try { // Check if xml has link to stylesheet
           System.out.println("Looking for XSL file...");
           xslsource = tfactory.getAssociatedStylesheet(saxsource,media, title, charset);
 
           // Set system-id
           if (xslsystemid != null)
             xslsource.setSystemId(xslsystemid);
 
           transformer = tfactory.newTransformer(xslsource);
 
           try {
            transformer.transform(saxsource, new javax.xml.transform.sax.SAXResult(handler));
           } catch (Exception e) { //Error in transforming
             System.err.println("Error in transformer.transform method...");
             System.err.println("source: "+saxsource.toString());
          }
                  
        } catch (Exception e) { //No stylesheet in xml
 
         if (xslfile != null)
           xslsource = new StreamSource(xslfile);
           if (xslsystemid != null)
             xslsystemid = xslfile.toString();
         }
 
              
         if (xslsource != null)
           transformer = tfactory.newTransformer(xslsource);
         else
           transformer = tfactory.newTransformer();
 
         transformer.transform(saxsource, new javax.xml.transform.sax.SAXResult(handler));
 

      }
 
}
 
 

Reply via email to