I made a get request and store the response in a String response:
HttpClient client = new DefaultHttpClient();
String getURL = "some_url_with_param_values";
HttpGet get = new HttpGet(getURL);
HttpResponse responseGet = client.execute(get);
HttpEntity resEntityGet = responseGet.getEntity();
String response = EntityUtils.toString(resEntityGet);
But I am only interested in the <div>s that has the class name <div class="product-data">. So, I did this:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
InputSource is;
builder = factory.newDocumentBuilder();
is = new InputSource(new StringReader(xml));
Document doc = builder.parse(is);
NodeList list = doc.getElementsByTagName("product-data"); //I even tried: (div class="product-data)
String test = list.item(0).getNodeValue(); //Just to test it
Unfortunately, it didn’t work. Any help will be appreciated.
My response string is basically an html page.
<!DOCTYPE html .....
<html>
<head>
//some script tags
</head>
<body>
//some tags
<div class="product-data">
//some other tags
</div>
//some tags
<div class="product-data">
//some other tags
</div>
....
</body>
</html>
I think you should try using
getElementsByClassName('product-data')If that doesn’t work you could always check Jsoup, it provides an library which gives an easy way of extracting Html elements from a webpage
This example executes an Http GET and then extracts all Div elements with the class “classname” which you can then do what you like with