验证(XSD)对XML的机器人 [英] Validator (xsd) for xml in android

查看:121
本文介绍了验证(XSD)对XML的机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有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.

这篇关于验证(XSD)对XML的机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆