我在网上搜索了几个小时,但我被困住了。
我使用了一个解析 xml 的教程,我得出的结论是最好使用 DOM xml 解析器。主要是因为我解析的内容只包含 30 项。
xml 如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<sliderlist>
<item>
<urlslide>http://www.google.com</urlslide>
</item>
<item>
<urlslide>http://www.stackoverflow.com</urlslide>
</item>
</sliderlist>
DOM 解析器(大部分来自教程):
TextView urlslide[];
try {
URL url = new URL("http://www.mydomain.com/my.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(url.openStream()));
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getElementsByTagName("item");
/** Assign textview array lenght by arraylist size */
urlslide = new TextView[nodeList.getLength()];
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
urlslide[i] = new TextView(this);
Element fstElmnt = (Element) node;
NodeList nameList = fstElmnt.getElementsByTagName("urlslide");
Element nameElement = (Element) nameList.item(0);
nameList = nameElement.getChildNodes();
urlslide[i].setText(((Node) nameList.item(0)).getNodeValue());
//layout.addView(urlslide[i]);
}
} catch (Exception e) {
System.out.println("XML Pasing Excpetion = " + e);
}
然后我需要解析内容的部分填充我的数组(现在使用 values/res 文件夹中的 xml):
mTimer=new CountDownTimer(15000, 1000) {
String[] myArray = getResources().getStringArray(R.array.testArray);
//String[] myArray={"http://www.upc.nl/","http://www.google.com","http://www.flyerwall.nl"};
//<- String should be read from landingpage.
int currentIndex=0;
public void onTick(long millisUntilFinished) {
//if (currentIndex<myArray.length) {
//number.setText("seconds remaining: " + millisUntilFinished / 1000);
//currentIndex++;
//}
}
public void onFinish() {
if (currentIndex<myArray.length) {
//number.setText("done!");
mWebView.loadUrl(myArray[currentIndex]);
currentIndex++;
} else {
currentIndex=0;//reset current url to 0
if (currentIndex<myArray.length)
//number.setText("done!");
mWebView.loadUrl(myArray[currentIndex]);
currentIndex++;
mTimer.start(); //activate the loop
}
mTimer.start();//first time start
}
};
我看了这么多示例代码,以至于我不再有任何线索,但我的胆量说它应该很容易完成。