Exception when calling SchemaFactory.newInstance on Android 4.0

I have an application for Android 4.0 that tries to call the following as recommended in the Android SDK:

SchemaFactory sf = SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI); 

but this gives me the following exception:

 java.lang.IllegalArgumentException: http://www.w3.org/2001/XMLSchema 

my code is:

 btnSearch.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v) { try{ //no code before this line and exception threw right here. SchemaFactory sf = SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI); .... } 
+4
source share
1 answer

I had a similar problem. Apparently, the constant depends on the base system, and I found that I couldnโ€™t get regular curls to work with Android, however I found Xerces-for-Android and it worked for me. The following are details of the installation and sample code. Good luck :)

The following worked for me:

  • Create a validation utility.
  • Get both xml and xsd to a file on Android OS and use the verification utility for it.
  • Use Xerces-For-Android to verify.

Android supports some packages that we can use, I created my xml checker utility based on: http://docs.oracle.com/javase/1.5.0/docs/api/javax/xml/validation/package-summary.html

My initial test sandbox was pretty smooth with java, then I tried passing it to Dalvik and found that my code was not working. Some things are not supported the same way with Dalvik, so I made some changes.

I found a link to xerces for android, so I changed the test to the sandbox (the following one does not work with android, an example after that ):

 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; } } 

The given code had to be changed to work with xerces for android ( http://gc.codehum.com/p/xerces-for-android/ ). To get the project you will need SVN, the following notes:

 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 

With the above jar (In the end I will make it into the jar, just copied it directly to my source for quick testing. If you want to do the same, you can quickly make the jar using Ant ( http://ant.apache.org /manual/using.html )), I was able to get the following for my XML validation:

 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; } } 

Some side notes:

To create files, I created a simple utility for writing strings to files:

 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(); } } 

I also needed to write to the area to which I had access, so I used:

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

A little hacky, it works. I am sure there is a more concise way to do this, however I decided that I would share my success, since there were no good examples that I found.

0
source

Source: https://habr.com/ru/post/1381071/


All Articles