- XML Reference Guide
- Overview
- What Is XML?
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Table of Contents
- The Document Object Model
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- DOM and Java
- Informit Articles and Sample Chapters
- Books and e-Books
- Implementations
- DOM and JavaScript
- Using a Repeater
- Repeaters and XML
- Repeater Resources
- DOM and .NET
- Informit Articles and Sample Chapters
- Books and e-Books
- Documentation and Downloads
- DOM and C++
- DOM and C++ Resources
- DOM and Perl
- DOM and Perl Resources
- DOM and PHP
- DOM and PHP Resources
- DOM Level 3
- DOM Level 3 Core
- DOM Level 3 Load and Save
- DOM Level 3 XPath
- DOM Level 3 Validation
- Informit Articles and Sample Chapters
- Books and e-Books
- Documentation and Implementations
- The Simple API for XML (SAX)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- SAX and Java
- Informit Articles and Sample Chapters
- Books and e-Books
- SAX and .NET
- Informit Articles and Sample Chapters
- SAX and Perl
- SAX and Perl Resources
- SAX and PHP
- SAX and PHP Resources
- Validation
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Document Type Definitions (DTDs)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XML Schemas
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- RELAX NG
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Schematron
- Official Documentation and Implementations
- Validation in Applications
- Informit Articles and Sample Chapters
- Books and e-Books
- XSL Transformations (XSLT)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XSLT in Java
- Java in XSLT Resources
- XSLT and RSS in .NET
- XSLT and RSS in .NET Resources
- XSL-FO
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XPath
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XML Base
- Informit Articles and Sample Chapters
- Official Documentation
- XHTML
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XHTML 2.0
- Documentation
- Cascading Style Sheets
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XUL
- XUL References
- XML Events
- XML Events Resources
- XML Data Binding
- Informit Articles and Sample Chapters
- Books and e-Books
- Specifications
- Implementations
- XML and Databases
- Informit Articles and Sample Chapters
- Books and e-Books
- Online Resources
- Official Documentation
- SQL Server and FOR XML
- Informit Articles and Sample Chapters
- Books and e-Books
- Documentation and Implementations
- Service Oriented Architecture
- Web Services
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Creating a Perl Web Service Client
- SOAP::Lite
- Amazon Web Services
- Creating the Movable Type Plug-in
- Perl, Amazon, and Movable Type Resources
- Apache Axis2
- REST
- REST Resources
- SOAP
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- SOAP and Java
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- WSDL
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- UDDI
- UDDI Resources
- XML-RPC
- XML-RPC in PHP
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Ajax
- Asynchronous Javascript
- Client-side XSLT
- SAJAX and PHP
- Ajax Resources
- JSON
- Ruby on Rails
- Creating Objects
- Ruby Basics: Arrays and Other Sundry Bits
- Ruby Basics: Iterators and Persistence
- Starting on the Rails
- Rails and Databases
- Rails: Ajax and Partials
- Rails Resources
- Web Services Security
- Web Services Security Resources
- SAML
- Informit Articles and Sample Chapters
- Books and e-Books
- Specification and Implementation
- XML Digital Signatures
- XML Digital Signatures Resources
- XML Key Management Services
- Resources for XML Key Management Services
- Internationalization
- Resources
- Grid Computing
- Grid Resources
- Web Services Resource Framework
- Web Services Resource Framework Resources
- WS-Addressing
- WS-Addressing Resources
- WS-Notifications
- New Languages: XML in Use
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Google Web Toolkit
- GWT Basic Interactivity
- Google Sitemaps
- Google Sitemaps Resources
- Accessibility
- Web Accessibility
- XML Accessibility
- Accessibility Resources
- The Semantic Web
- Defining a New Ontology
- OWL: Web Ontology Language
- Semantic Web Resources
- Google Base
- Microformats
- StructuredBlogging
- Live Clipboard
- WML
- XHTML-MP
- WML Resources
- Google Web Services
- Google Web Services API
- Google Web Services Resources
- The Yahoo! Web Services Interface
- Yahoo! Web Services and PHP
- Yahoo! Web Services Resources
- eBay REST API
- WordML
- WordML Part 2: Lists
- WordML Part 3: Tables
- WordML Resources
- DocBook
- Articles
- Books and e-Books
- Official Documentation and Implementations
- XML Query
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- XForms
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Resource Description Framework (RDF)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Topic Maps
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation, Implementations, and Other Resources
- Rich Site Summary (RSS)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- Simple Sharing Extensions (SSE)
- Atom
- Podcasting
- Podcasting Resources
- Scalable Vector Graphics (SVG)
- Informit Articles and Sample Chapters
- Books and e-Books
- Official Documentation
- OPML
- OPML Resources
- Summary
- Projects
- JavaScript TimeTracker: JSON and PHP
- The Javascript Timetracker
- Refactoring to Javascript Objects
- Creating the Yahoo! Widget
- Web Mashup
- Google Maps
- Indeed Mashup
- Mashup Part 3: Putting It All Together
- Additional Resources
- Frequently Asked Questions About XML
- What's XML, and why should I use it?
- What's a well-formed document?
- What's the difference between XML and HTML?
- What's the difference between HTML and XHTML?
- Can I use XML in a browser?
- Should I use elements or attributes for my document?
- What's a namespace?
- Where can I get an XML parser?
- What's the difference between a well-formed document and a valid document?
- What's a validating parser?
- Should I use DOM or SAX for my application?
- How can I stop a SAX parser before it has parsed the entire document?
- 2005 Predictions
- 2006 Predictions
- Nick's Book Picks
I hear a lot of complaining about SOAP, and about WSDL, about how it's too complicated, and too hard to use, and how the world would somehow be magically better if SOAP had never been invented. Look at Yahoo! Web Services, they say, they use REST, they're much simpler than Google's SOAP-based system.
Well, the complainers are right about one thing: Yahoo! Web Services does use REST. And if all you want is to request the data and look at it, then yes, it's simpler. But I like to do more than look at my data.
So having said all that, maybe you've got it in your head that using Google's API is going to be complicated, even difficult.
Well, you're wrong.
In fact, when I first set out to put together the sample application for this section, I'd intended for it to be a Java GUI application. I thought that might be a nice example of the fact that you can use these APIs to take data off the web. It wasn't long before I realized that actually using the API -- at least, the Java implementation of it that Google provides -- was only a tiny straightforward fraction of the code. Putting together the GUI was many times more complex in comparison.
So in the spirit of keeping things simple, let's build a small command-line application that requests the top 10 web pages for a particular query. Along the way we'll discuss the various options, and by the time we're through you'll have a good idea of how to incorporate this functionality into your own applications.
(You'll also see how easy it can be to use the kind of API you can generate from WSDL file. This API isn't directly generated, but it's the kind of thing that WSDL lets you do.)
First, let's talk about how this works in general terms. In order to display the items for a particular search, you need to execute these 5 steps:
- Create a "search" object.
- Set its credentials
- Set the query you want to run
- Execute the search
- Examine each record returned by the search, retrieving the appropriate data
Let's see it in action. Before you start, make sure to download the Google API from http://www.google.com.
First, we'll create the basic application:
public class GoogleWatch { public static void main(String args[]){ String keyword = args[0]; System.out.println("You entered "+keyword); } }
This application runs from the command line and takes a single argument, the query you want to run. (We'll talk more about the actual queries in a few moments.)
Next, we'll create the search and set it's credentials:
import com.google.soap.search.*; public class GoogleWatch { public static void main(String args[]){ String keyword = args[0]; GoogleSearch siteSearch = new GoogleSearch(); siteSearch.setKey("181gQtaOdlLD+83qvh6PnssQR5WlaB9d"); } }
Note that in order to compile and run this application, you will need to make sure that the classpath includes
the googleapi.jar
file downloaded with the SDK.
The actual key you're including with the setKey()
method is your Google license key.. If you don't
already have one, you'll need to go to http://www.google.com/apis
and request one. This key is how Google limits you to 1000 queries a day.
Next we set the query:
import com.google.soap.search.*; public class GoogleWatch { public static void main(String args[]){ String keyword = args[0]; GoogleSearch siteSearch = new GoogleSearch(); siteSearch.setKey("181gQtaOdlLD+83qvh6PnssQR5WlaB9d"); try { siteSearch.setQueryString(keyword); } catch (GoogleSearchFault f) { System.out.println("The call to the Google Web APIs failed:"); System.out.println(f.toString()); } } }
Note that you don't need to learn a whole new means of building queries, the web services API uses the exact same queries the web interface does. So I could simply search for sites that reference "Darth Vader," with a simple query of "Darth Vader" or I can search for sites that link to "The Darth Side," a fantastic piece of fan fiction, with a query of:
link:http://darthside.blogspot.com/
I could even look for syndicated feeds referencing the wayward Jedi:
darth vader (filetype:rss OR filetype:atom OR filetype:rdf)
Notice the boolean-type rules still apply, even though we're not using the web interface.
Now we're ready to perform the actual search:
import com.google.soap.search.*; public class GoogleWatch { public static void main(String args[]){ String keyword = args[0]; GoogleSearch siteSearch = new GoogleSearch(); siteSearch.setKey("181gQtaOdlLD+83qvh6PnssQR5WlaB9d"); try { siteSearch.setQueryString(keyword); GoogleSearchResult siteResults = siteSearch.doSearch(); } catch (GoogleSearchFault f) { System.out.println("The call to the Google Web APIs failed:"); System.out.println(f.toString()); } } }
The search returns a GoogleSearchResult
object, which contains not just the results themselves, but also information about actual the search. For example, we
can find out approximately how many results there are for a particular query:
import com.google.soap.search.*; public class GoogleWatch { public static void main(String args[]){ String keyword = args[0]; GoogleSearch siteSearch = new GoogleSearch(); siteSearch.setKey("181gQtaOdlLD+83qvh6PnssQR5WlaB9d"); try { siteSearch.setQueryString(keyword); GoogleSearchResult siteResults = siteSearch.doSearch(); int estimatedSites = siteResults.getEstimatedTotalResultsCount(); System.out.println("There are "+estimatedSites+" matches for the query "+keyword."); } catch (GoogleSearchFault f) { System.out.println("The call to the Google Web APIs failed:"); System.out.println(f.toString()); } } }
Note that although you can set the starting record for a search -- for example, siteSearch.setStart(50)
-
you can only retrieve results 1 through 1000. Anything farther down the list is not available through this interface.
The GoogleSearchResult
object also holds other useful information, such as
searchComments
, and searchTips
, which provide information intended for the
end user, estimateIsExact
, which is just what it sounds like, and directoryCategories
,
which is an array of Open Directory Project categories that match this particular search.
Then there are the results themselves. They are actually returned as an array of
GoogleSearchResultElement
objects, retrievable using the getResultElements(),
method:
import com.google.soap.search.*; public class GoogleWatch { public static void main(String args[]){ String keyword = args[0]; GoogleSearch siteSearch = new GoogleSearch(); siteSearch.setKey("181gQtaOdlLD+83qvh6PnssQR5WlaB9d"); try { siteSearch.setQueryString(keyword); GoogleSearchResult siteResults = siteSearch.doSearch(); int estimatedSites = siteResults.getEstimatedTotalResultsCount(); GoogleSearchResultElement[] sites = new GoogleSearchResultElement[10]; sites = siteResults.getResultElements(); if (sites.length == 0){ System.out.println("No results found for query '"+keyword+"'"); } else { for (int i = 0; i < sites.length; i++) { GoogleSearchResultElement thisSite = sites[i]; String title = thisSite.getTitle(); String snippet = thisSite.getSnippet(); String url = thisSite.getURL(); System.out.println(title); System.out.println(snippet); System.out.println(url); System.out.println("----------------------------"); } } } catch (GoogleSearchFault f) { System.out.println("The call to the Google Web APIs failed:"); System.out.println(f.toString()); } } }
For each result, we can pull specific information. For example, we've pulled the title
,
snippet
, and url
for each result. If we run the application, we'll see the results:
>java GoogleWatch "Darth Vader" Star Wars: Databank | <b>Vader</b>, <b>Darth</b> Character profile from the official Star Wars site. http://www.starwars.com/databank/character/darthvader/ ---------------------------- <b>Darth</b> <b>Vader</b> - Star Wars.com The official site of David Prowse, the actor who played <b>Darth</b> <b>Vader</b >. Includes<br> character information, biography, and store. http://www.darthvader-starwars.com/ ---------------------------- STAR WARS: Injuries of <b>Darth</b> <b>Vader</b> My father and <b>Darth</b> <b>Vader</b> fell into the crater and my father was i nstantly killed.<br> <b>...</b> At least one major fact has changed: Anakin Sky walker and <b>Darth</b> <b>Vader</b> <b>...</b> http://www.theforce.net/swtc/injuries.html ---------------------------- <b>Darth</b> <b>Vader</b> - Wikipedia, the free encyclopedia <b>Darth</b> <b>Vader</b> was born Anakin Skywalker and is the father of twins L uke Skywalker<br> <b>...</b> He is given the name "<b>Darth</b> <b>Vader</ b>" in 19 BBY. Throughout the six films, <b>...</b> http://en.wikipedia.org/wiki/Darth_Vader ---------------------------- WNC: <b>Darth</b> <b>Vader</b> Grotesque <b>Darth</b> <b>Vader</b> Drawing Word of the competition was spread nationwide through National<br> Geographic World Magazine. The third-place winner was Chri stopher Rader, <b>...</b> http://www.cathedral.org/cathedral/discover/darth.shtml ---------------------------- Star Wars - Star Wars <b>Darth</b> <b>Vader</b> Voice Changer Mask <b>...</b> speak into the helmetà your voice will sound just like <b>Darth</b> < b>Vader</b>Æs! Quick tips<br> to help achieve the best performance with the <b> DARTH</b> <b>VADER</b> VOICE CHANGER <b>...</b> http://www.hasbro.com/pl/page.viewproduct/product_id.15397/dn/starwars/default.c fm ---------------------------- 'boards - Screening Room <b>Darth</b> <b>Vader's</b> heart of evil is no match for Hollywood executiv es'. Credits:.<br> Agency: Mother Creatives: Yan Elliot, Luke Williamson Pr oducer: Angela Eleini <b>...</b> http://www.boardsmag.com/screeningroom/commercials/1659/ ---------------------------- The Morning News - <b>Darth</b> <b>Vader</b> Made Me Cry, by Matthew Baldwin From the first moment you see <b>Darth</b> <b>Vader</b> choke that rebel fighter , you know heÆs<br> <b>...</b> <b>Darth</b> <b>Vader</b> was on the second or t hird floorùI donÆt remember which, <b>...</b> http://www.themorningnews.org/archives/stories/darth_vader_made_me_cry.php ---------------------------- AtomFilms - <b>Darth</b> <b>Vader's</b> Psychic Hotline <b>Darth</b> <b>Vader's</b> Psychic Hotline United States | 4:59 | John E. H udgens <b>...</b> One phone<br> call solves them all with <b>Darth</b> <b>Vader 's</b> Psychic Hotline no matter whether <b>...</b> http://www.atomfilms.com/af/content/darth_vaders_psychic ---------------------------- James Earl Jones Voice of <b>Darth</b> <b>Vader</b>; "3rd Rock from the Sun" (1996) TV Series (uncredited) .<br> <b>...</b> <b>Darth</b> <b>Vader</b>; The Greatest Th ing That Almost Happened (1977) (TV) . <b>...</b> http://www.imdb.com/name/nm0000469/ ----------------------------
Notice that the results include the "bold" tag (<b></b>) around the actual keyword, so if you were displaying this information on a web page, you could easily tell why the result was included. Other information available for each result includes:
summary
: the Open Directory Sumary, if there is onecachedSize
: Google frequently saves a cached copy of pages it indexs. If there is such a copy, this property shows you how big it isrelatedInformationPresent
: True or false, this property tells you whether Google can provide related pages for this URL (via the related: search term)directoryCategory
anddirectoryTitle
: Information on how and where this URL is listed in the Open Directory Project, if applicable
Overall, the Google Web Service API is a great way to incorporate Google results into your web page or application, whether you use the API provided here or use another method, such as using XSLT to transform the data for your own purposes.