简体   繁体   中英

How to validate and parse an UTF-16 encoded XML file in Java?

I am using an UTF-16 encoded XML file. When i tried to validate this file using schema validation, the following exception is thrown:

org.xml.sax.SAXParseException: Content is not allowed in prolog.
    at    
com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.
    createSAXParseException(Unknown Source)

My code is given below.

SchemaFactor schemaFactory = 
  SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
/** load a WXS schema, represented by a Schema instance */
Source schemaFile = new StreamSource(new File("Sample.xsd"));
try {
    Schema schema = schemaFactory.newSchema(schemaFile);
    javax.xml.validation.Validator validator = schema.newValidator();
    validator.validate(new StreamSource(new File("Testing.xml")));
    System.out.println("Validation Successs");
} catch (SAXException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

Your xml is not valid. There is some content before the prolog. XML prolog is generally <?xml version="1.0"?> Fix your xml. No problem with the code.

在序言错误中允许的内容是因为它期望的是UTF-8 ...我只能通过将编码更改为UTF-8然后进行验证来修复它。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM