I m trying to extract content based on given xpath. When it is just one element i want to extract, there is no issue. When I have a list of items matching that xpath, then i get the nodelist and i can extract the values.
However, there are a couple items related to each other forming a group, and that group repeats itself.
One way I could do is to get the nodelist of parent node of all such groups and then apply SAX based parsing technique to extract information. But this would introduce pattern specific coding. I want to make it generic.
ex.
<html><body>
<!--... a lot divs and other tags ... -->
<div class="divclass">
<item>
<item_name>blah1</item_name>
<item_qty>1</item_qty>
<item_price>100</item_price>
</item>
</div>
<div class="divclass">
<item>
<item_name>blah2</item_name>
<item_qty>2</item_qty>
<item_price>200</item_price>
</item>
</div>
<div class="divclass">
<item>
<item_name>blah3</item_name>
<item_qty>3</item_qty>
<item_price>300</item_price>
</item>
</div>
</body></html>
I could easily write code for this xml but not a generic one which could parse any given specification.
I should be able to create a list of map of attribute-value from above.
Has anyone tried this?
EDIT
List of input xpaths:
1. "html:div[@class='divclass']/item/item_name"
2. "html:div[@class='divclass']/item/item_qty"
3. "html:div[@class='divclass']/item/item_price"
Expected output in simple text:
item_name:blah1;item_qty:1;item_price:100
item_name:blah2;item_qty:2;item_price:200
item_name:blah3;item_qty:3;item_price:300
Key point here is, if I apply each xpath separately, it would fetch me results vertically, i.e. first one will fetch all item_names, second will fetch all qtys. So I’ll loose the co-relation within these pieces.
Hope this clears my requirements.
Thanks
Nayn
I think XQuery is a great solution for screen scraping. You can use the Saxon processor for executing your xqueries. Moreover, you can use Piggy Bank Firefox extension to easily find the XPath expressions, regarding the content you want to extract from a web page, that you can use within your xqueries.