मुझे एक ही समस्या थी और मुझे जवाब देने से पहले कई पोस्ट पढ़ीं जो मेरे लिए काम करती थीं। निरंतर संदर्भ दल्विक पर काम नहीं करेगा। मैंने पाया कि मुझे ज़ीरस-टू-एंड्रॉइड प्रोजेक्ट के साथ काम करने के लिए अपना कोड संशोधित करना पड़ा था, फिर मैं एक्सएमएल सत्यापन प्राप्त करने में सक्षम था। जो वैरिएबल संदर्भ द्वारा आप कर रहे हैं सबसे अधिक संभावना है। निम्नलिखित सेटअप के विवरण हैं और कुछ उदाहरण कोड दिखा रहे हैं कि एंड्रॉइड पर काम करने के सत्यापन को कैसे प्राप्त किया जाए।
निम्नलिखित मेरे लिए काम किया:
- एक सत्यापन उपयोगिता बनाएँ।
- एंड्रॉइड ओएस पर फ़ाइल में एक्सएमएल और एक्सएसडी दोनों प्राप्त करें और इसके खिलाफ सत्यापन उपयोगिता का उपयोग करें।
- सत्यापन करने के लिए Xerces-for-Android का उपयोग करें।
एंड्रॉयड कुछ संकुल का समर्थन है जो हम उपयोग कर सकते हैं करता है, मैं के आधार पर मेरे एक्सएमएल सत्यापन उपयोगिता बनाया: http://docs.oracle.com/javase/1.5.0/docs/api/javax/xml/validation/package-summary.html
मेरे प्रारंभिक सैंडबॉक्स परीक्षण जावा के साथ बहुत चिकनी थी, तो मैं इसे पोर्ट पर करने के लिए Dalvik करने की कोशिश की और पाया कि मेरा कोड काम नहीं करता है। कुछ चीजें सिर्फ दल्विक के साथ समर्थित नहीं हैं, इसलिए मैंने कुछ संशोधन किए हैं।
मैं एंड्रॉयड के लिए xerces के लिए एक संदर्भ पाया, तो मैं की मेरी सैंडबॉक्स परीक्षण संशोधित (निम्नलिखित एंड्रॉयड साथ काम नहीं करता है, इस के बाद उदाहरण करता है):
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 के साथ काम करने के लिए कुछ संशोधित करने के लिए (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
ऊपर जार (अंततः मैं इसे एक जार में बना देंगे साथ
, बस इसे सीधे कॉपी किया त्वरित परीक्षण के लिए मेरे स्रोत में।
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;
}
}
कुछ साइड नोट्स::
आप भी ऐसा ही करना चाहते हैं, तो आप चींटी के साथ जल्दी से जार बना सकते हैं (
http://ant.apache.org/manual/using.html)), मैं अपने एक्सएमएल सत्यापन के लिए काम करने के लिए निम्नलिखित प्राप्त करने में सक्षम था
फ़ाइलें बनाने के लिए, मैं एक साधारण फ़ाइल उपयोगिता फ़ाइलों को स्ट्रिंग लिखने के लिए बनाया:
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;
थोड़ा हैकिश, यह काम करता है। मुझे यकीन है कि ऐसा करने का एक और संक्षिप्त तरीका है, हालांकि मुझे लगा कि मैं अपनी सफलता साझा करूंगा, क्योंकि मुझे कोई अच्छा उदाहरण नहीं मिला था।
मेरे पास प्लेटफॉर्म स्तर 9 और 10 के साथ एक ही समस्या है। जैसा कि [इस उत्तर] द्वारा कहा गया है (http://stackoverflow.com/questions/801632/android-schema/802150#802150), ऐसा लगता है कि वर्तमान में कोई नहीं है एंड्रॉइड में एक्सएमएल स्कीमा समर्थन। –
[इसी तरह दिखता है] (http://stackoverflow.com/questions/2694259/android-adt-eclipse-plugin-parsesdkcontent-failed) लेकिन उत्तर –