我在Android中有一个使用API​​ 10(2.3.3版)的项目,并且在用xsd文件验证xml时遇到问题。
这是我的代码:

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 Simulator抛出异常:
E / AndroidRuntime(4770):由以下原因引起:java.lang.IllegalArgumentException:http://www.w3.org/2001/XMLSchema

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

为什么??

最佳答案

因为您的平台不支持XML模式。

关于android - Android中XML的验证器(XSD),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10596540/

10-10 19:44