我有一个 xml 文件位于
http://androtrends.hostingsiteforfree.com/direct.xml
我正在尝试解析这个 xml 文件以在我的程序中使用它,但是每次程序强制关闭时,尽管它没有错误。我是 java 新手,请帮忙!
以下是我在 XMLParser.java 中使用的代码
public class XMLParser {
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;
}
//Parsing XML content and getting DOM element
public Document getDomElement(String xml){
Document doc = null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
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 DOM
return doc;
}
//Getting each xml child element value by passing element node name
public String getValue(Element item, String str) {
NodeList n = ((Document) item).getElementsByTagName(str);
return this.getElementValue(n.item(0));
}
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 "";
}
}
Channel.java的代码是
public class Channel extends ListActivity{
String selectedmode, channelName, channelLink;
Intent urlIntent;
// All static variables
static final String DirectURL = "http://androtrends.hostingsiteforfree.com/direct.xml";
static final String FlashURL="http://androtrends.hostingsiteforfree.com/direct.xml";
// XML node keys
static final String KEY_ITEM = "item"; // parent node
static final String KEY_Channel = "channel";
static final String KEY_Link = "link";
String xml;
Toast toast;
ArrayList<HashMap<String, String>> menuItems;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.channels);
selectedmode=getIntent().getExtras().getString("key");
urlIntent=new Intent(Intent.ACTION_VIEW);
Toast.makeText(this, "Wait 60 secs to load the stream...", Toast.LENGTH_LONG).show();
updatechannels();
ListView lv=getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
channelName= ((TextView) view.findViewById(R.id.channelName)).getText().toString();
channelLink= ((TextView) view.findViewById(R.id.channelLink)).getText().toString();
urlIntent.setData(Uri.parse(channelLink));
toast.show();
startActivity(urlIntent);
}
});
}
private void updatechannels() {
// TODO Auto-generated method stub
menuItems = new ArrayList<HashMap<String, String>>();
XMLParser parser = new XMLParser();
if (selectedmode.equalsIgnoreCase("direct"))
{
xml = parser.getXmlFromUrl(DirectURL); // getting XML
}else
{
xml = parser.getXmlFromUrl(FlashURL);
}
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_ITEM);
// 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_Channel, parser.getValue(e, KEY_Channel));
map.put(KEY_Link, parser.getValue(e, KEY_Link));
// adding HashList to ArrayList
menuItems.add(map);
}
// Adding menuItems to ListView
ListAdapter adapter = new SimpleAdapter(this, menuItems,
R.layout.list_item,
new String[] { KEY_Channel, KEY_Link }, new int[] {
R.id.channelName, R.id.channelLink });
setListAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}