
import java.io.InputStream;

import nu.xom.Builder;
import nu.xom.Document;
import nu.xom.Nodes;
import nux.xom.xquery.XQueryUtil;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;
import org.xml.sax.XMLReader;


public class VcardScrape {
	
	String query = "declare namespace xhtml=\"http://www.w3.org/1999/xhtml\"; \n" +
		"for $data in //xhtml:* \n"+
		" where contains($data/@class, \"vcard\") \n"+
		" return <vcard> \n" + 
			" <fn> { data($data//xhtml:*[contains(@class, \"fn\")]) } </fn>" +
			" <street-address> { data($data//xhtml:*[contains(@class, \"street-address\")]) } </street-address>" +
			" <locality> { data($data//xhtml:*[contains(@class, \"locality\")]) } </locality>" +
			" <region> { data($data//xhtml:*[contains(@class, \"region\")]) } </region>" +
			" <country-name> { data($data//xhtml:*[contains(@class, \"country-name\")]) } </country-name>" +
		" </vcard>  ";
	
	String url = "http://11870.com/pro/20770"; 
	
	public void processPage() {  
        
        try {
        	GetMethod get = new GetMethod(url); 
            get.setFollowRedirects(true); 
        	
        	HttpClient httpClient = new HttpClient();
            httpClient.executeMethod(get); 
            InputStream in = get.getResponseBodyAsStream(); 
        	
            XMLReader parser = new org.ccil.cowan.tagsoup.Parser();
            Document doc = new Builder(parser).build(in);
            Nodes results = XQueryUtil.xquery(doc, query);
            
            System.out.println( results.get(0).toXML() );
        } catch (Exception e) {
        	e.printStackTrace();
        }
    }
	
	
	public static void main(String []args)
	{
		VcardScrape review = new VcardScrape();
		review.processPage();
	}
}
