Android 验证xml,Android中的XML模式验证

@iOSDev,我不得不使用Xerces-for-Android进行验证。以下是我使用我的程序所做的工作的摘要:

创建验证实用程序。

将android和OS上的xml和xsd都放到文件中,并使用验证实用程序。

使用Xerces-For-Android进行验证。

Android确实支持我们可以使用的一些软件包,我根据以下内容创建了我的xml验证实用程序:http://docs.oracle.com/javase/1.5.0/docs/api/javax/xml/validation/package-summary.html

我最初的沙箱测试非常流畅,然后我尝试将其移植到Dalvik并发现我的代码不起作用。有些事情与Dalvik不相同,所以我做了一些修改。

我找到了一个对xerces for android的引用,所以我修改了我的沙盒测试(以下不适用于android,这之后的例子):

import java.io.File;

import javax.xml.XMLConstants;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.transform.Source;

import javax.xml.transform.dom.DOMSource;

import javax.xml.transform.stream.StreamSource;

import javax.xml.validation.Schema;

import javax.xml.validation.SchemaFactory;

import javax.xml.validation.Validator;

import org.w3c.dom.Document;

/**

* A Utility to help with xml communication validation.

*/

public class XmlUtil {

/**

* Validation method.

* Base code/example from: http://docs.oracle.com/javase/1.5.0/docs/api/javax/xml/validation/package-summary.html

*

* @param xmlFilePath The xml file we are trying to validate.

* @param xmlSchemaFilePath The schema file we are using for the validation. This method assumes the schema file is valid.

* @return True if valid, false if not valid or bad parse.

*/

public static boolean validate(String xmlFilePath, String xmlSchemaFilePath) {

// parse an XML document into a DOM tree

DocumentBuilder parser = null;

Document document;

// Try the validation, we assume that if there are any issues with the validation

// process that the input is invalid.

try {

// validate the DOM tree

parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();

document = parser.parse(new File(xmlFilePath));

// create a SchemaFactory capable of understanding WXS schemas

SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

// load a WXS schema, represented by a Schema instance

Source schemaFile = new StreamSource(new File(xmlSchemaFilePath));

Schema schema = factory.newSchema(schemaFile);

// create a Validator instance, which can be used to validate an instance document

Validator validator = schema.newValidator();

validator.validate(new DOMSource(document));

} catch (Exception e) {

// Catches: SAXException, ParserConfigurationException, and IOException.

return false;

}

return true;

}

}

上面的代码必须修改一些与xerces for android(http://gc.codehum.com/p/xerces-for-android/)一起使用。您需要SVN才能获得该项目,以下是一些小册子:

download xerces-for-android

download silk svn (for windows users) from http://www.sliksvn.com/en/download

install silk svn (I did complete install)

Once the install is complete, you should have svn in your system path.

Test by typing "svn" from the command line.

I went to my desktop then downloaded the xerces project by:

svn checkout http://xerces-for-android.googlecode.com/svn/trunk/ xerces-for-android-read-only

You should then have a new folder on your desktop called xerces-for-android-read-only

使用上面的jar(最终我将它变成一个jar,只需将它直接复制到我的源代码中进行快速测试。如果你想做同样的事情,你可以使用Ant快速制作jar(http://ant.apache .org / manual / using.html)),我能够获得以下工作来进行我的xml验证:

import java.io.File;

import java.io.IOException;

import mf.javax.xml.transform.Source;

import mf.javax.xml.transform.stream.StreamSource;

import mf.javax.xml.validation.Schema;

import mf.javax.xml.validation.SchemaFactory;

import mf.javax.xml.validation.Validator;

import mf.org.apache.xerces.jaxp.validation.XMLSchemaFactory;

import org.xml.sax.SAXException;

/**

* A Utility to help with xml communication validation.

*/public class XmlUtil {

/**

* Validation method.

*

* @param xmlFilePath The xml file we are trying to validate.

* @param xmlSchemaFilePath The schema file we are using for the validation. This method assumes the schema file is valid.

* @return True if valid, false if not valid or bad parse or exception/error during parse.

*/

public static boolean validate(String xmlFilePath, String xmlSchemaFilePath) {

// Try the validation, we assume that if there are any issues with the validation

// process that the input is invalid.

try {

SchemaFactory factory = new XMLSchemaFactory();

Source schemaFile = new StreamSource(new File(xmlSchemaFilePath));

Source xmlSource = new StreamSource(new File(xmlFilePath));

Schema schema = factory.newSchema(schemaFile);

Validator validator = schema.newValidator();

validator.validate(xmlSource);

} catch (SAXException e) {

return false;

} catch (IOException e) {

return false;

} catch (Exception e) {

// Catches everything beyond: SAXException, and IOException.

e.printStackTrace();

return false;

} catch (Error e) {

// Needed this for debugging when I was having issues with my 1st set of code.

e.printStackTrace();

return false;

}

return true;

}

}

一些附注:

为了创建文件,我创建了一个简单的文件实用程序来将字符串写入文件:

public static void createFileFromString(String fileText, String fileName) {

try {

File file = new File(fileName);

BufferedWriter output = new BufferedWriter(new FileWriter(file));

output.write(fileText);

output.close();

} catch ( IOException e ) {

e.printStackTrace();

}

}

我还需要写一个我可以访问的区域,所以我使用了:

String path = this.getActivity().getPackageManager().getPackageInfo(getPackageName(), 0).applicationInfo.dataDir;

有点hackish,它的工作原理。我确信有更简洁的方法可以做到这一点,但我想我会分享我的成功,因为我没有找到任何好的例子。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值