如何读取Android的XML文件 [英] How to read XML file in android

查看:85
本文介绍了如何读取Android的XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读它看起来像下面的XML文件... 我已经保存它的资产文件夹

I want to read xml file which looks like the following ... I have stored it in assets folder with

<ImageList SpriteSheetName="hh_gmw01">
   <Image Name="gmw01"     x="0" y="0" width="1047" height="752"/>
   <Image Name="hht1l01"   x="388" y="269" width="34" height="36"/>
   <Image Name="hht1l02"   x="147" y="99" width="85" height="33"/>
</ImageList>

我如何获得这些价值?

How do I get these values?

推荐答案

尝试了这一点首先:

import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class Main extends Activity {

Button btn;
TextView tvXml;

/** 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("textxml.xml");
            } catch (IOException e) {
                Log.e("tag", e.getMessage());
            }

            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();
}
}

这篇关于如何读取Android的XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆