0

我有一个问题,我需要从我的 sdcard 目录中读取一个 xml 文件并至少保存内容,然后在 xml 标记之间选择 de 值,但是如果您可以帮助我读取和转换为字符串,它将已经很有用了。

文件目录:Environment.getExternalStorageDirectory()+ "/SCity/"+now //currentmillis 文件名:Notes.xml

4

2 回答 2

0

您需要使用 SAX 或 DOM 解析器解析 xml。请点击以下链接:

http://developer.android.com/training/basics/network-ops/xml.html

http://www.ibm.com/developerworks/library/x-android/

于 2013-02-22T10:18:22.183 回答
-2

将您的文件放在资产文件夹中:

public class Main extends Activity {

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button btn = (Button) findViewById(R.id.button1);

    btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // Load XML for parsing
            AssetManager assetManager = getAssets();
            InputStream inputStream = null;
            try {
                inputStream = assetManager.open("test.xml");
            } catch (IOException e) {

            }

            String s = readTextFile(inputStream);
            TextView tv = (TextView)findViewById(R.id.textView1);
            tv.setText(s);
        }
    });
}

private String readTextFile(InputStream inputStream) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

    byte buf[] = new byte[1024];
    int len;
    try {
        while ((len = inputStream.read(buf)) != -1) {
            outputStream.write(buf, 0, len);
        }
        outputStream.close();
        inputStream.close();
    } catch (IOException e) {

    }
    return outputStream.toString();
  }
}
于 2013-04-28T19:01:39.650 回答