简体   繁体   English

遍历XML文件不会返回所有结果

[英]Looping through XML file doesn't return all results

I am trying to parse an XML document I obtained from LinkedIn using their API (sample response is here: http://developer.linkedin.com/documents/get-network-updates-and-statistics-api ). 我正在尝试使用他们的API解析从LinkedIn获得的XML文档(示例响应在这里: http : //developer.linkedin.com/documents/get-network-updates-and-statistics-api )。

In my XML response, I have more than one "updates," though for whatever reason my for loop only prints the first update instead of cycling through all 250 or so of them. 在我的XML响应中,我有多个“更新”,尽管出于某种原因,我的for循环仅打印第一个更新,而不是循环遍历所有250个左右的更新。

    Response response = request.send(); 
    stream = response.getStream();

    System.out.println(response.getBody());

    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();

    try 
    {
        DocumentBuilder dbBuilder = dbFactory.newDocumentBuilder();
        Document doc = dbBuilder.parse(stream);
        doc.getDocumentElement().normalize();

        System.out.println("root of xml: " + doc.getDocumentElement().getNodeName());
        NodeList nodes = doc.getElementsByTagName("updates");

        for (int i=0; i < nodes.getLength(); i++)
        {
            Node node = nodes.item(i); 

            if (node.getNodeType() == Node.ELEMENT_NODE) 
            {
                /** 
                 * Let's get the update type first, and then determine how we should proceed 
                 * if we are CONN, VIRL, etc.
                 */

                Element element = (Element) node;
                String update_type = getValue("update-type", element);

                if (update_type.equals("CONN"))
                {
                    String url = getValue("url", element); 
                    String user = getValue("first-name", element) + " " + getValue("last-name", element);

                    getConnectedProfile(url);
                }   
            }
        }

    } catch (ParserConfigurationException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SAXException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 

Thank you. 谢谢。

NodeList nodes = doc.getElementsByTagName("updates");

Should be 应该

NodeList nodes = doc.getElementsByTagName("update");

You are only getting 1 element because there is only 1 "updates" tag. 您只有1个元素,因为只有1个“更新”标签。 Thus, the loop is only iterating once. 因此,该循环仅迭代一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM