-->

XMLSchema validation on Android

2019-04-03 18:39发布

问题:

I maintain the JDOM project and I am trying to 'certify' it on Android. Most things work fine, but XML Schema validation is proving problematic...

My questions are: Is there a way to do XMLSchema validation on Android? If there is, how?

Questions like this have been asked before, but no conclusive answer is given:

  • Android schema validation
  • XML Schema Validation in Android

This is what I currently 'know' (correct me if I am wrong)...:

  • SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema") - fails with IllegalArgumentException
  • the default 'ExpatParser' in Android from the Harmony project does not support XMLSchema validation.
  • It is possible to load Xerces in an Android app, and even parse with it, but not to do a Validating parse - when you try you get 'FATAL' Android exceptions causing application crashes.

I have been working on this for some time now, and I have put together the following 'research':

  • JDOM2 and Android
  • JDOM2 - Android with Xerces

If anyone has any more information about XMLSchema Validation on Android I would greatly appreciate any input at all.

If anyone has successfully done XMLSchema validation on XML and can help me get the functionality working for JDOM they'll get thousands of internet points ... ;-) and will be immortalized in the JDOM code and commit messages.

回答1:

I'm fascinated that, after several years, this still is an open issue. There only seems to be bad news, though. According to the AOSP Issue Tracker Schema validation currently seems unsupported with the standard Android APIs and Google seems to be unwilling to fix it:

Our XML APIs (including SAX, XmlPull and DOM) don't support any of the following:

 XML Schema 

 XML DTDs (including external entity declarations and references)
 validation

 element content whitespace

However, one commenter of that same ticket references a workaround, and provides example code using a Xerces port. I don't know if this goes beyond what you've figured out, so far, but I hope it helps.

To wrap this up: The SchemaFactoryFinder only knows the following Schema definitions:

  • W3C_XML_SCHEMA10_NS_URI = "http://www.w3.org/XML/XMLSchema/v1.0"
  • W3C_XML_SCHEMA10_NS_URI = "http://www.w3.org/XML/XMLSchema/v1.1"

Using any other schema definition causes it to fail (It will however log this on debug level). Obviously this is the case for you, as you're using another reference to the 2011 schema. So "correctly" referencing the Schema Definition should fix this issue.



回答2:

I am using

javax.xml.parsers.DocumentBuilder;
javax.xml.parsers.DocumentBuilderFactory;
javax.xml.parsers.ParserConfigurationException;

If you got a XML string you can parse it by:

Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
doc = db.parse(is);

please don't forget the exception handling.



回答3:

I think you could use RelaxNG - there are plenty of validators available. http://relaxng.org/#validators

Of particular interest should be - http://www.kohsuke.org/relaxng/bali/doc/ - https://msv.java.net/ - http://www.davidashen.net/rnv.html

The latter is C implementation, the first two are written in Java.

If you need high performance, you could write some JNI code and call functions in the rnv source. A simpler approach would be to just build rnv for Android, using NDK and then call it's executable with parameters.

Something like

Process p = Runtime.exec("/path/to/rnv/exec", [valdidationDoc: String, some more params]);
OutputStream out = p.getOutputStream(); // connected to STDIN of p
InputStream in = p.getInputStream(); // connected to STDOUT of p
out.write(new FileInputStream("/path/to/xml"));

/// read in for succes/errors