gpt4 book ai didi

java - Android中从URL读取XML文件

转载 作者:行者123 更新时间:2023-12-01 04:38:52 24 4
gpt4 key购买 nike

我尝试了多种从链接打开 XML 文件的方法,但所有方法都没有任何效果。为了确保解析 XML 文件的效果良好,我下载了该文件并将其放入 Asset 文件夹中,以便解析所有这些内容。由此我们可以推断出读取url时出现的错误

网址为http://feeds.bbci.co.uk/sport/0/football/rss.xml?edition=uk

HttpClient httpclient = new DefaultHttpClient();
HttpGet httppost = new HttpGet(url.toString());
HttpResponse response = httpclient.execute(httppost);
InputStream in=response.getEntity().getContent();

或者

HttpURLConnection con=(HttpURLConnection)url.openConnection();
InputStream in=con.getInputStream();

或者

InputStream in =url.openStream();

请帮帮我

我的问题已经解决,感谢大家的帮助。当我从 Url 获取文件时,我应该使用 AsyncTask 类

最佳答案

public void xmlReader() {

try {

URL url = new URL("http://feeds.bbci.co.uk/sport/0/football/rss.xml");

DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory
.newDocumentBuilder();
Document document = documentBuilder.parse(new InputSource(url
.openStream()));

nodlist = document.getElementsByTagName("Youritem");

for (int i = 0; i < nodlist.getLength(); i++) {
Element element = (Element) nodlist.item(i);

NodeList nodlistid = element.getElementsByTagName("Id");
Element id = (Element) nodlistid.item(0);


NodeList nodlistBaslik = element.getElementsByTagName("tagname1");
Element baslik = (Element) nodlistBaslik.item(0);

NodeList nodlistDetay = element.getElementsByTagName("tagname2");
Element detay = (Element) nodlistDetay.item(0);

NodeList nodlistKaynak = element.getElementsByTagName("tagname3");
Element lat = (Element) nodlistKaynak.item(0);

NodeList nodelistMedia = element.getElementsByTagName("tagname4");
Element longi = (Element) nodelistMedia.item(0);

NodeList nodelistTur = element.getElementsByTagName("tagname5");
Element tur = (Element) nodelistTur.item(0);
// String resimURL =
// resim.getAttributes().getNamedItem("url").getNodeValue();

NodeList nodelistMedia1 = element.getElementsByTagName("enclosure");
Element picture= (Element) nodelistMedia1.item(0);
String pictureURL = resim.getAttributes().getNamedItem("picturetagname").getNodeValue();

xmltagname1.add(tagname1.getChildNodes().item(0).getNodeValue());
xmltagname2.add(tagname2.getChildNodes().item(0).getNodeValue());
xmltagname3.add(tagname3.getChildNodes().item(0).getNodeValue());
xmltagname4.add(tagname4.getChildNodes().item(0).getNodeValue());
xmltagname5.add(tagname5.getChildNodes().item(0).getNodeValue());


}

} catch (Exception e) {
e.printStackTrace();
}

}

我认为这个方法可以帮助你..放轻松

关于java - Android中从URL读取XML文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16940564/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com