我正在尝试使用他们的API解析我从LinkedIn获得的XML文档(示例响应在这里:http://developer.linkedin.com/documents/get-network-updates-and-statistics-api)。
在我的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();
}
谢谢。
答案 0 :(得分:0)
NodeList nodes = doc.getElementsByTagName("updates");
应该是
NodeList nodes = doc.getElementsByTagName("update");
您只获得1个元素,因为只有1个“更新”标记。因此,循环只迭代一次。