2016-06-15 11 views
-2

私はXMLを持っています。そして、このXMLはe XSDで検証する必要があります。 XSDはISO-8859-1エンコーディングを要求しました。ISO-8859-1でXSDスキーマを検証する

私はこのコードを試しましたが、動作しません。誰かに不具合を見なさいか。

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; 
    } 
} 

そして

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

+0

エンコーディングで見つめXMLファイルである理由

購入に出力手段を設定しますXSDのXML宣言では、XSDを使用するXMLファイルのエンコーディングとは関係ありません。指定されている限り、エンコーディングを使用することも、デフォルト(UTF-8)を使用することもできます。 – mjn

答えて

0

は、以下の方法のようにしてくださいXSDからトップ:

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")); 

    } 
} 

output :(if it is validated) true

私はそれがあなたを助けることを望みます..

+0

こんにちは、ご協力いただきありがとうございます。しかし、今私はこのエラーがあります '2016-06-15 11:26:13,952エラー:XMLを検証しません java.lang.UnsupportedOperationException:このパーサーは "null"バージョン "null"の仕様をサポートしていません – Deceptio

+0

あなたのxsdを上記? – san544

+0

どのようなJDKバージョンを使用していますか? – san544

0

私はMetohd validateXMLを持っています。そして私は "setEncoding(" ISO-8859-1 ")" 終わり

<?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); 

} 
関連する問題