Android中读取XML响应 [英] reading XML response in android
本文介绍了Android中读取XML响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图读取并从下列URL XML响应
http://sagt.vizushop.com/DefaultSimple.aspx?command=default
I'm trying to read and the XML response from the following url http://sagt.vizushop.com/DefaultSimple.aspx?command=default
我用下面的code在我的Android项目获得的第一个项目,然后第2节点的值。
I use the following code in my android project to get the first item and then its 2nd node's value.
String uri="http://sagt.vizushop.com/DefaultSimple.aspx?command=default";
String [] data=new String [9];
try{
URL url = new URL(uri);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.setDoOutput(true);
con.connect();
data = new String[9];
if (con.getResponseCode() == HttpURLConnection.HTTP_OK) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(con.getInputStream());
NodeList itemnodes=doc.getElementsByTagName("item");
Node firstitemnode=itemnodes.item(0);
NodeList childnodesoffirstitemnode=firstitemnode.getChildNodes();
Node firstnodeOffirstitem=childnodesoffirstitemnode.item(1);
String contenturl=firstnodeOffirstitem.getNodeValue().toString();
data[0] = contenturl;
}
但它给出了一个显示java.lang.NullPointerException。
But it gives a java.lang.NullPointerException.
请在此帮助
感谢提前
推荐答案
请使用下code使用DOM解析器解析XML。
Please Use below code for XML parsing using Dom Parser.
public class XMLParsingDOMExample extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/** Create a new layout to display the view */
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(1);
/** Create a new textview array to display the results */
TextView id[];
TextView imageurl[];
try {
URL url = new URL("http://sagt.vizushop.com/DefaultSimple.aspx?command=default");
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 */
id = new TextView[nodeList.getLength()];
imageurl = new TextView[nodeList.getLength()];
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
id[i] = new TextView(this);
imageurl[i] = new TextView(this);
Element fstElmnt = (Element) node;
NodeList idList = fstElmnt.getElementsByTagName("item_id");
Element idElement = (Element) idList.item(0);
idList = idElement.getChildNodes();
id[i].setText("id is = " + ((Node) idList.item(0)).getNodeValue());
NodeList imageurlList = fstElmnt.getElementsByTagName("item_image");
Element imageurlElement = (Element) imageurlList.item(0);
imageurlList = imageurlElement.getChildNodes();
imageurl[i].setText("imageurl is = " + ((Node) imageurlList.item(0)).getNodeValue());
layout.addView(id[i]);
layout.addView(imageurl[i]);
}
} catch (Exception e) {
System.out.println("XML Pasing Excpetion = " + e);
}
/** Set the layout view to display */
setContentView(layout);
}
}
,看看下面的链接了解更多信息。
And See below link for more information.
这篇关于Android中读取XML响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文