I have below xml layout from which I am suppose to extract all the "userid" value which is inside <key> </key>
and load them to HashSet
in Java
<?xml version="1.0" encoding="UTF-8"?>
<response>
<plds>
<fld>consumerid</fld>
<fld>last_set</fld>
</plds>
<record>
<data>934463448 1417753752</data>
<key_data>
<key>
<name>userid</name>
<value>934463448</value>
</key>
</key_data>
</record>
<record>
<data>1228059948 1417753799</data>
<key_data>
<key>
<name>userid</name>
<value>1228059948</value>
</key>
</key_data>
</record>
</response>
I will be getting above xml data from a url and it is possible that I can get big XML file. What is the best way to parse the above XML and extract all the "userid" and load it in the HashSet in Java?
This is what I have started -
public static Set<String> getUserList(String host, String count) {
Set<String> usrlist = new HashSet<String>();
String url = "urlA"; // this url will return me above XML data
InputStream is = new URL(url).openStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
// not sure what I should do here which can
// parse my above xml and extract all the
// userid and load it into usrlist hash set
return usrlist;
}
UPDATE:-
This is what I have tried -
public static Set<String> getUserList() {
Set<String> usrlist = new HashSet<String>();
String url = "urlA"; // this url will return me above XML data
InputStream is = new URL(url).openStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new URL(url).openStream());
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr = xpath.compile("//record/key_data/key[name='userid']/value");
NodeList nodes = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
for (int i = 0; i < nodes.getLength(); i++) {
usrlist.add(nodes.item(i).getNodeValue());
}
return usrlist;
}
But I am not getting any user id in the usrlist
object? Anything wrong I am doing here?