【问题标题】:Android remote XML parse (document builder)Android 远程 XML 解析(文档生成器)
【发布时间】:2012-09-27 12:53:44
【问题描述】:

我已经上网好几天了,我真的需要一些帮助,我正在尝试将 XML 文档从 Web 服务器解析为 android 中的 ListView,我已经弄清楚如何使用本地文件和这很好,但无论我在堆栈或其他网站上发现什么,它似乎都不起作用,谁能帮我解决这个问题?我知道该页面存在并且有效...我现在把我所有的头发都拔掉了,所以请帮忙:)

下面是我的方法代码,在 on create 和项目的 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();
     }

我错过了什么愚蠢的东西还是我完全错过了球?任何人都可以向我指出会有所帮助的信息,或者只是让我知道:)

干杯!!

【问题讨论】:

  • 有人有这方面的信息吗?

标签: java android xml parsing dom


【解决方案1】:

我已经做了那个任务,你应该这样做:

                //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);

等等……

希望对你有帮助,

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-10
    • 1970-01-01
    • 2020-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多