我已经在互联网上搜索了好几天了,我真的需要一些帮助,我正在尝试将 XML 文档从 Web 服务器解析到 Android 中的 ListView 中,我已经弄清楚了如何使用本地文件来执行此操作,并且这很好,但无论我在堆栈或其他网站上找到什么,它似乎都不起作用,任何人都可以帮助我解决这个问题吗?我知道该页面存在并且可以工作...我现在把我的头发都拔光了,所以请帮忙:)
下面是我的方法代码,这是在创建项目和 onclicklistener 之后调用的,我也使用文档生成器工厂方法。
private void xmlparse()
{
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = dbf.newDocumentBuilder();
Document doc = builder.parse(new URL("URL in here").openConnection().getInputStream());
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("item");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
titles.add(getTagValue(TITLE_1, eElement)); //TITLE_1 is the xml tag title
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
private String getTagValue(String sTag, Element eElement) {
NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
Node nValue = (Node) nlList.item(0);
return nValue.getNodeValue();
}
我是否错过了一些愚蠢的东西或者我完全错过了球?任何人都可以向我指出有帮助的信息或只是让我知道:)
干杯!!
最佳答案
我已经做了这个任务,你应该执行以下操作:
//Formattage des strings pour le passage en URL
from = URLEncoder.encode(from, "UTF-8");
to = URLEncoder.encode(to, "UTF-8");
String addressToConnect = "http://maps.googleapis.com/maps/api/distancematrix/xml?origins="+from+"&destinations="+to+"&mode=driving&units="+unit+"&sensor=false";
//Partie connexion
URL url = new URL(addressToConnect);
HttpURLConnection connexion = (HttpURLConnection) url.openConnection();
connexion.connect();
InputSource geocoderResultInputSource;
geocoderResultInputSource = new InputSource(connexion.getInputStream());
//Partie XML/XPATH
Document geocoderResultDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(geocoderResultInputSource);
XPath xpath = XPathFactory.newInstance().newXPath();
//On s'assure que la requete envoy�e � l'API � bien renvoy�e un STATUS = OK cf. Google API
NodeList nodeListCodeResult = (NodeList) xpath.evaluate("//status", geocoderResultDocument, XPathConstants.NODESET);
等等...
希望对你有帮助
关于java - Android 远程 XML 解析(文档生成器),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12761964/