使用ISO-8859-1验证XSD架构

时间:2016-06-15 08:36:37

标签: java xml validation iso-8859-1

我有一个XML。这个XML我必须用e XSD验证。并且XSD要求ISO-8859-1编码。

我已尝试使用此代码,但它无效。看到有人的错?

    public boolean validateXML(Document doc) throws ParserConfigurationException, IOException {

    XMLOutputter xmlOutput = new XMLOutputter();
    String xml = xmlOutput.outputString(doc);

    try {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setValidating(true);
        factory.setNamespaceAware(true);

        final SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

        factory.setValidating(true);
        final Schema schema = sf.newSchema(new StreamSource(getClass().getResourceAsStream(SCHEMA_PATH)));
        factory.setSchema(schema);

        factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema");
        DocumentBuilder builder = factory.newDocumentBuilder();

        InputSource is = new InputSource(xml);
        is.setEncoding("ISO-8859-1");

        builder.parse(is);
        return true;
    } catch (ParserConfigurationException pce) {
        throw pce;
    } catch (IOException io) {
        throw io;
    } catch (SAXException se) {
        return false;
    }
}

来自XSD的顶部

<?xml version='1.0' encoding='ISO-8859-1' ?>

       

2 个答案:

答案 0 :(得分:0)

按以下方式进行:

    public class XMLUtils {

  private XMLUtils() {}

  // validate SAX and external XSD 
  public static boolean validateWithExtXSDUsingSAX(String xml, String xsd) 
  throws ParserConfigurationException, IOException 
  {
    try {
      SAXParserFactory factory = SAXParserFactory.newInstance();
      factory.setValidating(false); 
      factory.setNamespaceAware(true);

      SchemaFactory schemaFactory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
      SAXParser parser = null;
      try {
         factory.setSchema(schemaFactory.newSchema(new Source[] {new StreamSource( xsd )}));
         parser = factory.newSAXParser();
      }
      catch (SAXException se) {
        System.out.println("SCHEMA : " + se.getMessage());  // problem in the XSD itself
        return false;
      }

      XMLReader reader = parser.getXMLReader();
      reader.setErrorHandler(
          new ErrorHandler() {
            public void warning(SAXParseException e) throws SAXException {
              System.out.println("WARNING: " + e.getMessage()); // do nothing
            }

            public void error(SAXParseException e) throws SAXException {
              System.out.println("ERROR : " + e.getMessage());
              throw e;
            }

            public void fatalError(SAXParseException e) throws SAXException {
              System.out.println("FATAL : " + e.getMessage());
              throw e;
            }
          }
          );
      reader.parse(new InputSource(xml));
      return true;
    }    
    catch (ParserConfigurationException pce) {
      throw pce;
    } 
    catch (IOException io) {
      throw io;
    }
    catch (SAXException se){
      return false;
  }
}

public static void main (String args[]) throws Exception{ 
    System.out.println
        (XMLUtils.validateWithExtXSDUsingSAX
            ("c:/temp/YourXML.xml", "c:/temp/YourXSD.xsd"));

  }
}
  

输出:(如果已验证)true

我希望它能帮到你......

答案 1 :(得分:0)

我有Metohd validateXML。我将Outputter设置为&#34; setEncoding(&#34; ISO-8859-1&#34;)&#34;

为什么最后是XML文件盯着

<?xml version="1.0" encoding="UTF-8"?>

Methode validateXML

private void validateXML(Document doc) throws Exception {

    XMLOutputter xmlOutput = new XMLOutputter(Format.getPrettyFormat().setEncoding("ISO-8859-1"));

    String docStr = xmlOutput.outputString(doc);

    //InputStream inputStream = new ByteArrayInputStream(docStr.getBytes(Charset.forName("ISO-8859-1")));
    byte[] bytes = docStr.getBytes(Charset.forName("ISO-8859-1"));
    InputStream inputStream = new ByteArrayInputStream(bytes);

    Source xml = new StreamSource(inputStream);
    File xsdfile = new File(this.getClass().getClassLoader().getResource(SCHEMA_PATH).getFile());

    SchemaFactory schemaFactor = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = schemaFactor.newSchema(xsdfile);


    Validator validator = schema.newValidator();

    validator.validate(xml);

}