我尝试使用 android studio 生成 XML 文件,但收到此错误:
Caused by: javax.xml.datatype.DatatypeConfigurationException: Provider org.apache.xerces.jaxp.datatype.DatatypeFactoryImpl not found.
问题来自“JAXBContext”行。 我有一个函数可以生成 XML 文件,其代码是
try {
File file = new File("D:\\Github\\Comedu\\file.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Resultat.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
// output pretty printed
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(this, file);
jaxbMarshaller.marshal(this, System.out);
} catch (JAXBException e) {
e.printStackTrace();
}
在 android studio 上,我将 xerces 包添加到我的构建路径中,所以我不知道如何解决这个问题。
最佳答案
如果你是一名Android开发者,正如文档所说注意,你必须提供自己的实现(例如Xerces); Android 未附带默认实现。
https://developer.android.com/reference/javax/xml/datatype/DatatypeFactory.html#newInstance%28%29
因此,尝试添加依赖项xercesImpl
Maven:
<!-- https://mvnrepository.com/artifact/xerces/xercesImpl -->
<dependency>
<groupId>xerces</groupId>
<artifactId>xercesImpl</artifactId>
<version>2.12.0</version>
</dependency>
Gradle :
// https://mvnrepository.com/artifact/xerces/xercesImpl
compile group: 'xerces', name: 'xercesImpl', version: '2.12.0'
关于java - Android Studio DatatypeFactoryImpl 未找到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51348471/