-1
我想寫解析XML代碼的代碼,並在列表視圖中寫的每個節點的值XML解析的Android
我與這個XML
http://www.iqraa.com/rss/CustomizedServices.aspx?Service=TvGuide
工作我嘗試按照本教程
http://www.androidhive.info/2011/11/android-xml-parsing-tutorial
,但沒有工作
任何幫助,請:(
這是XMLParser中的代碼
公共XMLParser類{
public XMLParser() {
}
/**
* Getting XML from URL making HTTP request
*
* @param url
* string
* */
public String getXmlFromUrl(String url) {
String xml = null;
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// return XML
return xml;
}
/**
* Getting XML DOM element
*
* @param XML
* string
* */
public Document getDomElement(String xml) {
Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
// dbf.setCoalescing(true);
try {
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xml));
doc = db.parse(is);
} catch (ParserConfigurationException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (SAXException e) {
Log.e("Error: ", e.getMessage());
return null;
} catch (IOException e) {
Log.e("Error: ", e.getMessage());
return null;
}
return doc;
}
/**
* Getting node value
*
* @param elem
* element
*/
public final String getElementValue(Node elem) {
Node child;
if (elem != null) {
if (elem.hasChildNodes()) {
for (child = elem.getFirstChild(); child != null; child = child
.getNextSibling()) {
if (child.getNodeType() == Node.TEXT_NODE) {
return child.getNodeValue();
}
}
}
}
return "";
}
/**
* Getting node value
*
* @param Element
* node
* @param key
* string
* */
public String getValue(Element item, String str) {
NodeList n = item.getElementsByTagName(str);
return this.getElementValue(n.item(0));
}
}
,這是在MainActivity的代碼
公共類MainActivity擴展ListActivity {
static final String URL = "http://iqraa.com/rss/CustomizedServices.aspx?Service=TvGuide";
// XML node keys
static final String KEY_PITEM = "ProgramItem"; // parent node
static final String KEY_PNAME = "ProgramName";
static final String KEY_LINK = "Link";
static final String KEY_STARTTIME = "StartTime";
static final String KEY_CHANNEL = "Channel";
static final String KEY_SCHEDULE = "ScheduleDate";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML
Log.i("Inside 1 Oncreate", "On create done");
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_PITEM);
// looping through all item nodes <item>
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
// adding each child node to HashMap key => value
map.put(KEY_PNAME, parser.getValue(e, KEY_PNAME));
//map.put(KEY_LINK, parser.getValue(e, KEY_LINK));
Log.i("Inside 1 Oncreate", "FOR LOOOOOOP ");
map.put(KEY_STARTTIME,parser.getValue(e, KEY_STARTTIME));
map.put(KEY_CHANNEL,parser.getValue(e, KEY_CHANNEL));
map.put(KEY_SCHEDULE, parser.getValue(e, KEY_SCHEDULE));
// adding HashList to ArrayList
menuItems.add(map);
}
// Adding menuItems to ListView
ListAdapter adapter = new SimpleAdapter(this, menuItems,
R.layout.list_item,
new String[] { KEY_PNAME, KEY_STARTTIME, KEY_CHANNEL}, new int[] {
R.id.name, R.id.stime, R.id.channel });
Log.i("Inside 3 Oncreate", "2222222");
setListAdapter(adapter);
// selecting single ListView item
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
Log.i("Inside 1 Oncreate", "44444444");
String name = ((TextView) view.findViewById(R.id.name)).getText().toString();
String channel = ((TextView) view.findViewById(R.id.channel)).getText().toString();
String stime = ((TextView) view.findViewById(R.id.stime)).getText().toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
in.putExtra(KEY_PNAME, name);
in.putExtra(KEY_CHANNEL, channel);
in.putExtra(KEY_STARTTIME, stime);
startActivity(in);
}
});
}
}
請出示你已經嘗試了代碼,並在那裏你遇到問題明確告訴我們。 –
發佈你的代碼,你已經做了什麼,你已經做了什麼,你的logcat輸出。沒有這些,社區中沒有人可以幫助你。 –
_但沒有什麼work_是不是很有幫助的傢伙。發佈你迄今爲止工作過的代碼。 –