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. Thus, the loop is only iterating once.
Domanda
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).
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.
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.
Soluzione
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. Thus, the loop is only iterating once.