【问题标题】:How Writing XMl file usning XMLStreamWriter in android如何在 android 中使用 XMLStreamWriter 编写 XML 文件
【发布时间】:2011-07-07 18:09:11
【问题描述】:

我想在这里创建 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)

【问题讨论】:

    标签: android


    【解决方案1】:

    XMLStreamWriter 在javax.xml.stream.* 包中。此包不包含在默认的 Android 构建中。你可以使用它,但你必须在你的应用程序中重新打包它。

    您的问题描述以及如何重新打包 javax.* 包的指南:http://code.google.com/p/dalvik/wiki/JavaxPackages

    如果您不想在您的应用中重新打包 XMLStreamWriter,请尝试使用 Android 中包含的 XmlSerializer。很好的例子就在这里:Writing XML on Android

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-28
      • 2011-09-17
      • 1970-01-01
      • 1970-01-01
      • 2013-09-02
      • 2020-07-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多