-1

我正在寻找在 Android 中将数组转换为 XML 文件的解决方案。如果您有示例代码或只有说明,您能提供给我吗?我会非常感激。预先感谢。托马斯·勒

4

1 回答 1

1

此示例代码可能会帮助您:

 protected string ConvertToXML(Object[] args, string rootName, string elemName)
    {
        string xmlStr = "<" + rootName + ">";

        foreach (Object arg in args)
        {
            xmlStr += "<" + elemName + ">" + arg.ToString() +
                      "</" + elemName + ">";
        }

        xmlStr += "</" + rootName + ">";

        return xmlStr;
    }

以下是调用函数的方法:

Object[] test = { "One", "Two", 3, "Four", 5, "six" };
Response.Write(ConvertToXML(test, "params", "param"));

这应该是输出:

<params>
    <param>One</param>
    <param>Two</param>
    <param>3</param>
    <param>Four</param>
    <param>5</param>
    <param>Six</param>
</params>

更新:将其保存到 sd 卡下面的代码可能会有所帮助:

String filename = "filename.txt";
File file = new File(Environment.getExternalStorageDirectory(), filename);
FileOutputStream fos;
byte[] data = new String("data to write to file").getBytes();
try {
    fos = new FileOutputStream(file);
    fos.write(data);
    fos.flush();
    fos.close();
} catch (FileNotFoundException e) {
    // handle exception
} catch (IOException e) {
    // handle exception
}

并且您必须将此行添加到清单文件中:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
于 2013-03-28T03:53:15.343 回答