验证(XSD)对XML的机器人机器人、XSD、XML

2023-09-05 23:58:16 作者:橘子酱萝莉

我有10 API(2.3.3版)在Android的一个项目,我对XML与XSD文件验证的一个问题。 这是我的code:

I have a project with API 10 (2.3.3 version) in Android and i have a problem for the validation of xml with xsd file. This is my code:

public static Document buildDoc(String xml, String xsd){
     // parse an XML document into a DOM tree
    Document document = null;

   try {            

       DocumentBuilderFactory parserFactory = DocumentBuilderFactory.newInstance();
        parserFactory.setNamespaceAware(true);
        DocumentBuilder parserdb = parserFactory.newDocumentBuilder();
        doc = parserdb.parse(new InputSource( new StringReader(xml)  ));

       SchemaFactory factory = SchemaFactory.newInstance(
                      XMLConstants.W3C_XML_SCHEMA_NS_URI);     //here the emulator raises an exception


        Source schemaFile = new StreamSource(new File(xsd));
        Schema schema = factory.newSchema(schemaFile);


        Validator validator = schema.newValidator();

        // validate the DOM tree
        validator.validate(new DOMSource(doc));
        System.out.println("Validation OK!");
    } catch (SAXException e) {
        // instance document is invalid!
             System.err.println("Validation ERROR!");
            e.printStackTrace();

    } catch (ParserConfigurationException e) {
                    // TODO Auto-generated catch block
             System.err.println("Validation ERROR!");
                    e.printStackTrace();
    } catch (IllegalArgumentException e) {
                    // TODO Auto-generated catch block
             System.err.println("Validation ERROR!");              
            } catch (IOException e) {
                    // TODO Auto-generated catch block
                     System.err.println("Validation ERROR!");
                    e.printStackTrace();

            }

    return doc;

}

我的Eclipse模拟器抛出一个异常: E / AndroidRuntime(4770):java.lang.IllegalArgumentException异常:产生的原因 http://www.w3.org / 2001 / XML模式

My Eclipse Simulator throw an exception: E/AndroidRuntime(4770): Caused by: java.lang.IllegalArgumentException: http://www.w3.org/2001/XMLSchema

在这一行: SchemaFactory的工厂= SchemaFactory.newInstance(                           XMLConstants.W3C_XML_SCHEMA_NS_URI);

In this line: SchemaFactory factory = SchemaFactory.newInstance( XMLConstants.W3C_XML_SCHEMA_NS_URI);

为什么?

推荐答案

由于XML模式不支持您的平台。

Because XML Schema isn't supported on your platform.