在下面的代码中,尝试在 DOM 中解析 XML 时出现空白屏幕。我也没有在清单中设置任何权限......请帮助!我有我的清单权利。
它工作正常......
package com.example.techgeek;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import android.os.*;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
public class second extends ListActivity {
String name;
public static String pass,pass2;
String[] getname=new String[100];
Element element2;Node node;NodeList nList;
ArrayList<String> list = new ArrayList<String>();
ArrayList<String> list2 = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
try {
//InputStream is = getAssets().open("file.xml");
URL url = new URL("http://sre.hostoi.com/xml_eg.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
//Document doc = dBuilder.parse(is);
Document doc = dBuilder.parse(new InputSource(url.openStream()));
Element element=doc.getDocumentElement();
element.normalize();
nList = doc.getElementsByTagName("employee");
for (int i=0; i < nList.getLength(); i++) {
node = nList.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
element2 = (Element) node;
list.add((getValue("name", element2)+"\n"));
list2.add((getValue("salary", element2)+"\n"));
}
}
} catch (Exception e) {
e.printStackTrace();
}
setListAdapter (new ArrayAdapter<String>(
second.this, android.R.layout.simple_list_item_1,list));
}
private static String getValue(String tag, Element element) {
NodeList nodeList = element.getElementsByTagName(tag).item(0).getChildNodes();
Node node = (Node) nodeList.item(0);
return node.getNodeValue();
}