0

問題が発生しました。sdcard ディレクトリから xml ファイルを読み取り、少なくとも内容を保存してから、xml タグ間の値を選択する必要がありますが、読み取りと文字列への変換を手伝っていただければ、すでに非常に便利です。

ファイルディレクトリ: 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 に答える