我想创建 XML 文件,这是我的以下代码
String fileName = "jasstech.xml";
XMLOutputFactory xof = XMLOutputFactory.newInstance();
XMLStreamWriter xtw = null;**
try
{
xtw = xof.createXMLStreamWriter(new FileOutputStream(fileName), "UTF-8");
xtw.writeStartDocument("UTF-8", "1.0");
xtw.writeStartElement("root");
xtw.writeComment("This is an attempt to create an XML file with StAX");
xtw.writeStartElement("foo");
xtw.writeAttribute("order", "1");
xtw.writeStartElement("meuh");
xtw.writeAttribute("active", "true");
xtw.writeCharacters("The cows are flying high this Spring");
xtw.writeEndElement();
xtw.writeEndElement();
xtw.writeStartElement("bar");
xtw.writeAttribute("order", "2");
xtw.writeStartElement("tcho");
xtw.writeAttribute("kola", "K");
xtw.writeCharacters("Content of tcho tag");
xtw.writeEndElement();
xtw.writeEndElement();
xtw.writeEndElement();
xtw.writeEndDocument();
}
catch (XMLStreamException e)
{
e.printStackTrace();
}
catch (IOException ie)
{
ie.printStackTrace();
}
以上
相同的代码在 JAVA 项目中运行良好
但在 Android 项目中出现以下错误
03-03 07:48:40.778: ERROR/AndroidRuntime(719): Uncaught handler: thread main exiting due to uncaught exception
03-03 07:48:40.788: ERROR/AndroidRuntime(719): com.kochar.xml.stream.FactoryConfigurationError: Provider com.ctc.wstx.stax.WstxOutputFactory not found
03-03 07:48:40.788: ERROR/AndroidRuntime(719): at com.kochar.xml.stream.XMLOutputFactory.newInstance(XMLOutputFactory.java:23)
XMLStreamWriter
在里面javax.xml.stream.*
包裹。该软件包不包含在默认的 Android 版本中。您可以使用它,但必须将其重新打包到您的应用程序中。
问题描述以及如何重新打包 javax.* 包的指南:http://code.google.com/p/dalvik/wiki/JavaxPackages http://code.google.com/p/dalvik/wiki/JavaxPackages
如果你不想重新打包XMLStreamWriter
在您的应用程序中,尝试使用XmlSerializer
它包含在 Android 中。很好的例子就在这里:在 Android 上编写 XML https://stackoverflow.com/questions/2290945/writing-xml-on-android/2296051#2296051
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)