gpt4 book ai didi

android,从SD卡的xml文件中读取值

转载 作者:行者123 更新时间:2023-11-29 21:52:50 25 4
gpt4 key购买 nike

helo,我想从这个文件中读取值:http://www.nbp.pl/kursy/xml/lastC.xml并用它们填充网格。我的应用程序每次运行时都会下载此文件并将其保存在 SDCard 上。我的问题是我的代码读取了空值。这是:

 @Override
protected void onCreate(Bundle savedInstanceState) {

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

grid= (GridView)findViewById(R.id.grid);

try{
url = new URL("http://www.nbp.pl/kursy/xml/LastC.xml");

HttpURLConnection connection = (HttpURLConnection)url.openConnection();

connection.setRequestMethod("GET");


connection.setDoOutput(true);
connection.connect();



File dir = new File (android.os.Environment.getExternalStorageDirectory().getAbsolutePath() + "/Kursy Walut");
if(dir.exists()==false)
dir.mkdirs();

File file = new File(dir, "kursywalut.xml");

FileOutputStream fileoutput = new FileOutputStream(file);

InputStream inputstream = connection.getInputStream();

int tempSize=0;


byte[] buffer = new byte[1024];

while((tempSize = inputstream.read(buffer))>0)
{

fileoutput.write(buffer, 0, tempSize);

}

fileoutput.close();

将文件保存在我的 sdCard 上并用信息做一个 toast

    Toast.makeText(MainActivity.this, "pobrano do Kursy Walut/kursywalut.xml!!", Toast.LENGTH_SHORT).show();

//here i have a array of string witch i will parse into my grid

String[] items=new String[13];

InputStream in = new FileInputStream(file.getPath());

DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();

Document doc = builder.parse(in);
NodeList words = doc.getElementsByTagName("pozycja");

for(int i=0; i<words.getLength(); i++)
{
items[i]= ((Element)words.item(i)).getAttribute("kod_waluty");

}

//i setText on some textView to check that i read xmlfile correctly.

text.setText("value: "+items[5]);
in.close();



grid.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, items));
}
catch(Throwable t){Toast.makeText(this, t.toString(), Toast.LENGTH_LONG).show();}


}

在这段代码之后,我的 TextView 文本只是“值:”而没有来自 xml 的值 我的网格是空的。我假设我在阅读 xml 文件时犯了错误。如果有人能看到这个并帮助我解决它,我会非常高兴。

最佳答案

请使用以下代码使用 Dom Parser 从 SDCard 解析 XML 文件,它将解决您的问题。

MainActivity.java:-

public class MainActivity extends Activity {

ArrayList<String> mImageLink;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

try {
mImageLink = new ArrayList<String>();

File file = new File("mnt/sdcard/kursywalut.xml");
InputStream is = new FileInputStream(file.getPath());
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(is));
doc.getDocumentElement().normalize();

NodeList nodeList = doc.getElementsByTagName("image");

for (int i = 0; i < nodeList.getLength(); i++) {

Node node = nodeList.item(i);

Element fstElmnt = (Element) node;

mImageLink.add(fstElmnt.getAttribute("link"));

}
} catch (Exception e) {
System.out.println("XML Pasing Excpetion = " + e);
}
}
}

关于android,从SD卡的xml文件中读取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14002974/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com