저는 Xerces-for-Android가 나를 위해 작동하도록했습니다.
- 유효성 검사 유틸리티를 만들기 : 나는 그렇게 잘하면 다음은 충분히 :
에게 내가 사용하는 일반적인 아이디어를 제공합니다 ... 웹에 더 유용한 참조 거의와 같은 문제가 있었다.
- xml과 xsd를 모두 Android OS에서 파일로 가져 와서 유효성 검사 유틸리티를 사용하십시오.
- 유효성 검사를 수행하려면 Android 용 Xerces를 사용하십시오.
, 나는 기반으로 내 XML 검증 유틸리티를 만들어 안드로이드는 우리가 사용할 수있는 몇 가지 패키지를 지원합니다 : 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;
}
}
위의 코드는 있었다 android (http://gc.codehum.com/p/xerces-for-android/)의 xerces로 작업 할 수 있도록 일부를 수정해야합니다. 단지 빠른 테스트를 위해 내 소스에 직접 복사, 위의 항아리 (결국 내가 항아리로 만들어 줄게으로
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
: 당신은 다음 몇 가지 침대 노트, SVN 프로젝트를 얻을 필요가있다.
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는)), 나는 내 XML 검증을 위해 일하려면 다음을 얻을 수 있었다 파일을 만들기위한
, 내가 파일에 문자열을 쓸 수있는 간단한 파일 유틸리티를 만든 :
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;
조금 해킹되어 작동합니다. 이 일을하는 더 간결한 방법이있을 것이라고 확신하지만, 내가 찾은 좋은 사례가 없기 때문에 성공을 나누겠다고 생각했습니다.
해결책이 있습니까? –
죄송하지만 Android에서 더 이상 작동하지 않습니다. –