Utilizar Bing com o java - Problema com a classe Document

2 respostas
F

Estudando um pouco sobre como usar o bing atravez do java vi um exemplo de codigo que me deixou com uma duvida. Ele faz a requisiçao de uma pesquisa e retorna um objeto do tipo Document, depois utiliza outro metodo para mostrar o resultado e é ai que esta o meu problema.

private static void DisplayResults(Document doc) throws XPathExpressionException 
	{
		String version = (String)xpath.evaluate("//@Version",doc,XPathConstants.STRING);
		String searchTerms = (String)xpath.evaluate("//api:SearchTerms",doc,XPathConstants.STRING);
		int total = Integer.parseInt((String)xpath.evaluate("//web:Web/web:Total",doc,XPathConstants.STRING));
		int offset = Integer.parseInt((String)xpath.evaluate("//web:Web/web:Offset",doc,XPathConstants.STRING));
		NodeList results = (NodeList)xpath.evaluate("//web:Web/web:Results/web:WebResult",doc, XPathConstants.NODESET); 

		// Display the results header.
		System.out.println("Live Search API Version " + version);
		System.out.println("Web results for " + searchTerms);
		System.out.println("Displaying " + (offset+1) + " to " + (offset + results.getLength()) + " of " + total + " results ");
		System.out.println();

		// Display the Web results.
		StringBuilder builder = new StringBuilder();

		for(int i = 0 ; i < results.getLength(); i++)
		{
			NodeList childNodes = results.item(i).getChildNodes();

			for (int j = 0; j < childNodes.getLength(); j++) 
			{
				if(!childNodes.item(j).getLocalName().equalsIgnoreCase("DisplayUrl"))
				{
					String fieldName = childNodes.item(j).getLocalName();

					if(fieldName.equalsIgnoreCase("DateTime"))
					{
						fieldName = "Last Crawled";
					}

					builder.append(fieldName + ":" + childNodes.item(j).getTextContent());
					builder.append("\n");
				}
			}

			builder.append("\n");
		}

		System.out.println(builder.toString());
	}

Dessa forma ele retorna apenas os 10 primeiros resultados, como fazer para pegar todos ?

2 Respostas

Leozin

Como que tu fez a chamada?

Esse teu código só lê o resultado não?

F

Consegui resolver, eu fazi a chamada assim:

public String BuildRequest(String pesquisa)
	{
		String requestString = "http://api.search.live.net/xml.aspx?"
			+ "AppId=" + AppId
			+ "&Query=" + pesquisa
			+ "&Sources=Web"
			+ "&Web.Count=50"

		return requestString;
	}

era só adicionar o offset ficando assim:

public String BuildRequest(String pesquisa, int offset)

{

String requestString = "<a href="http://api.search.live.net/xml.aspx?%22">http://api.search.live.net/xml.aspx?"</a>

+ “AppId=” + AppId

+ &Query=” + pesquisa

+ &Sources=Web”

+ &Web.Count=50”

+ &Web.Offset=” + offset;
return requestString;
}
Criado 21 de fevereiro de 2010
Ultima resposta 26 de fev. de 2010
Respostas 2
Participantes 2