Program Yahoo! with Java
Java’s native support for working with XML makes parsing Yahoo! Search Web Services responses a snap.
Simple REST, XML-over-HTTP interfaces like the one Yahoo! provides are most often associated with scripting languages such as Perl or PHP, while Java—a compiled language—is viewed as a better tool for working with more complex web services protocols. If Java is your preferred language, some new tools in the latest version have made it even easier to work with services like the Yahoo! API.
Java 1.5 added native support for working with XML documents using XPath. This means that if you know the XML format ahead of time, you can retrieve a specific piece of the document with a simple query. For example, every Yahoo! response includes an attribute called
totalResultsAvailable in the <ResultSet> tag, which holds the value of the total number of results Yahoo! has for that particular query. If you’d like to grab this value with XPath, you can use this simple query:
/ResultSet/@totalResultsAvailable
This hack presents a quick example using Java’s built-in XML and XPath tools to work with Yahoo! Search Web Services. This native support means you won’t need to download any external XML parsers; you simply need the latest version of Java.
What You Need
If you don’t have Java 1.5 or higher, you’ll need the latest Java Developer Kit (JDK™), available at http://java.sun.com. On the right side of the page, you’ll find a section called Popular Downloads. Click the link for ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access