XML 在网站中解析
XML Parse in website
我正在尝试解析 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 中,我有互联网许可!
谢谢!
您的代码示例有效。 Java 区分大小写。您确定您的 XML 标签名称正确吗?
这是一个使用 w3schools XML 示例的工作示例:
try {
URL url = new URL("http://www.w3schools.com/xml/note.xml");
URLConnection conn = url.openConnection();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(conn.getInputStream());
NodeList nodes = doc.getElementsByTagName("note");
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());
System.out.println(element.getTextContent());
}
} catch (Exception e) {
e.printStackTrace();
}
现在使用您的 XML 示例和 Joop Eggen's 答案:
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);
String airport = element.getAttribute("airport");
System.out.println("element " + i + ": airport=" + airport);
}
} catch (Exception e) {
e.printStackTrace();
}
使用简单文件的示例:
test.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>
</FLIGHTS>
Example.java:
import java.io.File;
import java.io.FileInputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
public class Example {
public static void main(String[] args) {
File f = new File("test.xml");
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new FileInputStream(f));
NodeList nodes = doc.getElementsByTagName("FLIGHT");
for (int i = 0; i < nodes.getLength(); i++) {
Element element = (Element) nodes.item(i);
String airport = element.getAttribute("airport");
System.out.println("element " + i + ": airport='" + airport + "'");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
如果 XML 是正确的,属性 XXX="..."
会有所不同:
Element element = (Element) nodes.item(i);
String airport = element.getAttribute("airport");
if (airport != null) { ...
我正在尝试解析 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 中,我有互联网许可!
谢谢!
您的代码示例有效。 Java 区分大小写。您确定您的 XML 标签名称正确吗?
这是一个使用 w3schools XML 示例的工作示例:
try {
URL url = new URL("http://www.w3schools.com/xml/note.xml");
URLConnection conn = url.openConnection();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(conn.getInputStream());
NodeList nodes = doc.getElementsByTagName("note");
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());
System.out.println(element.getTextContent());
}
} catch (Exception e) {
e.printStackTrace();
}
现在使用您的 XML 示例和 Joop Eggen's 答案:
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);
String airport = element.getAttribute("airport");
System.out.println("element " + i + ": airport=" + airport);
}
} catch (Exception e) {
e.printStackTrace();
}
使用简单文件的示例:
test.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>
</FLIGHTS>
Example.java:
import java.io.File;
import java.io.FileInputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
public class Example {
public static void main(String[] args) {
File f = new File("test.xml");
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new FileInputStream(f));
NodeList nodes = doc.getElementsByTagName("FLIGHT");
for (int i = 0; i < nodes.getLength(); i++) {
Element element = (Element) nodes.item(i);
String airport = element.getAttribute("airport");
System.out.println("element " + i + ": airport='" + airport + "'");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
如果 XML 是正确的,属性 XXX="..."
会有所不同:
Element element = (Element) nodes.item(i);
String airport = element.getAttribute("airport");
if (airport != null) { ...