org.xml.sax.SAXParseException: Content is not allowed in prolog

前端 未结 30 1785
别那么骄傲
别那么骄傲 2020-11-22 02:54

I have a Java based web service client connected to Java web service (implemented on the Axis1 framework).

I am getting following exception in my log file:

相关标签:
30条回答
  • 2020-11-22 03:37

    If all else fails, open the file in binary to make sure there are no funny characters [3 non printable characters at the beginning of the file that identify the file as utf-8] at the beginning of the file. We did this and found some. so we converted the file from utf-8 to ascii and it worked.

    0 讨论(0)
  • 2020-11-22 03:38

    I took code of Dineshkumar and modified to Validate my XML file correctly:

    import org.apache.log4j.Logger;
    
    public class Myclass{
    
    private static final Logger LOGGER = Logger.getLogger(Myclass.class);
    
    /**
     * Validate XML file against Schemas XSD in pathEsquema directory
     * @param pathEsquema directory that contains XSD Schemas to validate
     * @param pathFileXML XML file to validate
     * @throws BusinessException if it throws any Exception
     */
    public static void validarXML(String pathEsquema, String pathFileXML) 
    	throws BusinessException{	
    	String W3C_XML_SCHEMA = "http://www.w3.org/2001/XMLSchema";
    	String nameFileXSD = "file.xsd";
    	String MY_SCHEMA1 = pathEsquema+nameFileXSD);
    	ParserErrorHandler parserErrorHandler;
    	try{
    		SchemaFactory schemaFactory = SchemaFactory.newInstance(W3C_XML_SCHEMA);
    		
    		Source [] source = { 
    			new StreamSource(new File(MY_SCHEMA1))
    			};
    		Schema schemaGrammar = schemaFactory.newSchema(source);
    
    		Validator schemaValidator = schemaGrammar.newValidator();
    		schemaValidator.setErrorHandler(
    			parserErrorHandler= new ParserErrorHandler());
    		
    		/** validate xml instance against the grammar. */
    		File file = new File(pathFileXML);
    		InputStream isS= new FileInputStream(file);
    		Reader reader = new InputStreamReader(isS,"UTF-8");
    		schemaValidator.validate(new StreamSource(reader));
    		
    		if(parserErrorHandler.getErrorHandler().isEmpty()&& 
    			parserErrorHandler.getFatalErrorHandler().isEmpty()){
    			if(!parserErrorHandler.getWarningHandler().isEmpty()){
    				LOGGER.info(
    				String.format("WARNING validate XML:[%s] Descripcion:[%s]",
    					pathFileXML,parserErrorHandler.getWarningHandler()));
    			}else{
    				LOGGER.info(
    				String.format("OK validate  XML:[%s]",
    					pathFileXML));
    			}
    		}else{
    			throw new BusinessException(
    				String.format("Error validate  XML:[%s], FatalError:[%s], Error:[%s]",
    				pathFileXML,
    				parserErrorHandler.getFatalErrorHandler(),
    				parserErrorHandler.getErrorHandler()));
    		}		
    	}
    	catch(SAXParseException e){
    		throw new BusinessException(String.format("Error validate XML:[%s], SAXParseException:[%s]",
    			pathFileXML,e.getMessage()),e);
    	}
    	catch (SAXException e){
    		throw new BusinessException(String.format("Error validate XML:[%s], SAXException:[%s]",
    			pathFileXML,e.getMessage()),e);
    	}
    	catch (IOException e) {
    		throw new BusinessException(String.format("Error validate XML:[%s], 
    			IOException:[%s]",pathFileXML,e.getMessage()),e);
    	}
    	
    }
    
    }

    0 讨论(0)
  • 2020-11-22 03:39

    Try with BOMInputStream in apache.commons.io:

    public static <T> T getContent(Class<T> instance, SchemaType schemaType, InputStream stream) throws JAXBException, SAXException, IOException {
    
        JAXBContext context = JAXBContext.newInstance(instance);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        Reader reader = new InputStreamReader(new BOMInputStream(stream), "UTF-8");
    
        JAXBElement<T> entry = unmarshaller.unmarshal(new StreamSource(reader), instance);
    
        return entry.getValue();
    }
    
    0 讨论(0)
  • 2020-11-22 03:41

    This is often caused by a white space before the XML declaration, but it could be any text, like a dash or any character. I say often caused by white space because people assume white space is always ignorable, but that's not the case here.


    Another thing that often happens is a UTF-8 BOM (byte order mark), which is allowed before the XML declaration can be treated as whitespace if the document is handed as a stream of characters to an XML parser rather than as a stream of bytes.

    The same can happen if schema files (.xsd) are used to validate the xml file and one of the schema files has an UTF-8 BOM.

    0 讨论(0)
  • 2020-11-22 03:41

    Actually in addition to Yuriy Zubarev's Post

    When you pass a nonexistent xml file to parser. For example you pass

    new File("C:/temp/abc")
    

    when only C:/temp/abc.xml file exists on your file system

    In either case

    builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
    document = builder.parse(new File("C:/temp/abc"));
    

    or

    DOMParser parser = new DOMParser();
    parser.parse("file:C:/temp/abc");
    

    All give the same error message.

    Very disappointing bug, because the following trace

    javax.servlet.ServletException
        at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
    ...
    Caused by: org.xml.sax.SAXParseException: Content is not allowed in prolog.
    ... 40 more
    

    doesn't say anything about the fact of 'file name is incorrect' or 'such a file does not exist'. In my case I had absolutely correct xml file and had to spent 2 days to determine the real problem.

    0 讨论(0)
  • 2020-11-22 03:41

    What i have tried [Did not work]

    In my case the web.xml in my application had extra space. Even after i deleted ; it did not work!.

    I was playing with logging.properties and web.xml in my tomcat, but even after i reverted the error persists!.

    Solution

    To be specific i tried do adding

    org.apache.catalina.filters.ExpiresFilter.level = FINE

    Tomcat expire filter is not working correctly

    0 讨论(0)
提交回复
热议问题