Search code examples
androidxmlcompatibilitybackwards-compatibility

Supporting my Android 2.2 application on Android 2.1


I have developed an application in Android 2.2 and my clients want me to support it on 2.1 So I have some dependency for XML parsing as I am using following code:

XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
//Set xmlns namespace otherwise xmlns will not be parsed
xpath.setNamespaceContext(new PersonalNamespaceContext());

//Parse XML string
XPathExpression expr = xpath.compile(CCRKEY1);          
Object result = expr.evaluate(doc, XPathConstants.NODESET); 

Now in 2.1 I cannot access XPathFactory, XPath, XPathExpression So what could be the similar methods to replace these in 2.1??

Thanks,


Solution

  • XPath support wasn't present in 2.1, so I think you'll have to do the parsing yourself via SAX or DOM, or use an external XPath lib, like jaxen for instance. You could also use the JAXP library, which contains the official XPath implementation present in Java >5.

    You can also keep both the methods, and use Build.VERSION to get the version under which the app is running.