我有这段代码,但 toast 不会显示任何消息我的代码有什么问题。我可以从链接、linknext 获取值,但标题不会显示任何值。 (我不太擅长编写代码,所以请提出您可能喜欢的任何建议。
final Button button = (Button) findViewById(R.id.Button01);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Perform action on click
try {
URL url = new URL(
"http://somelink.com=" + Link.setFirst_link);
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 */
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
Element fstElmnt = (Element) node;
NodeList nameList = fstElmnt.getElementsByTagName("link");
Element nameElement = (Element) nameList.item(0);
nameList = nameElement.getChildNodes();
String img = (((Node) nameList.item(0)).getNodeValue());
NodeList websiteList = fstElmnt.getElementsByTagName("linknext");
Element websiteElement = (Element) websiteList.item(0);
websiteList = websiteElement.getChildNodes();
String nextlink = (((Node) websiteList.item(0)).getNodeValue());
Link.setFirst_link = nextlink;
Drawable drawable = LoadImageFromWebOperations(img);
imgView.setImageDrawable(drawable);
NodeList titleList = fstElmnt.getElementsByTagName("title");
Element titleElement = (Element) titleList.item(0);
websiteList = titleElement.getChildNodes();
String title = (((Node) titleList.item(0)).getNodeValue());
Context context = getApplicationContext();
CharSequence text = title;
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
} catch (Exception e) {
System.out.println("XML Pasing Excpetion = " + e);
}
}
});
/** Set the layout view to display
*/ }
这是 xml 文件
<?xml version="1.0"?>
<maintag>
<item>
<link>http://image.com/357769.jpg?40</link>
<linknext>http://www.image.com</linknext>
<title>imagename</title>
</item>
</maintag>
最佳答案
Element titleElement = (Element) titleList.item(0);
titleList = titleElement.getChildNodes(); // <<< here you wrote websiteList instead (copy & paste error I think ;)
关于java - 解析 XML 不会显示所有项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4715047/