我正在尝试用 XML 解析网页并在我的 logcat 中打印数据,但我的输出为空。这是我的 XML 结构:
<?xml version="1.0"?>
<FLIGHTS>
<FLIGHT airport="b: 3 "
logo="IG"
code="IG"
numero="1234"
carrier="AirBerlin"
city="NEW YORK"
terminal="Terminal A"
sched="08:40"
expect="09:09"
tipo_volo="L" stato="J"
</FLIGHT>
<FLIGHT airport="c: 3 "
....
.....more...
</FLIGHT>
</FLIGHTS>
这是我在 AsyncTask 中的 Android 代码:
@Override
protected String doInBackground(String... params) {
try {
URL url = new URL("http://myurl");
URLConnection conn = url.openConnection();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(conn.getInputStream());
NodeList nodes = doc.getElementsByTagName("FLIGHT");
for (int i = 0; i < nodes.getLength(); i++) {
Element element = (Element) nodes.item(i);
// NodeList title = element.getElementsByTagName("airport");
// Element line = (Element) title.item(0);
// a.add(line.getTextContent());
Log.d("LOG...", "" + element.getTextContent());
}
} catch (Exception e) {
e.printStackTrace();
}
在 AndroidManifest 中,我有互联网权限!
谢谢!