I'm trying to figure out how to get the following XML parsed to a Java class in Eclipse: http://finance.yahoo.com/webservice/v1/symbols/aapl/quote?format=xml&view=detail
I tried it following this tutorial, but I can't get it to work.
I want the name (Apple Inc.) and the price to be parsed. Here is my Quote Class (tried it with and without annotation parameters):
import java.io.Serializable;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "Quote")
public class Quote implements Serializable {
private String name;
private Integer price;
public String getName() {
return name;
}
@XmlElement(name = "name")
public void setName(String name) {
this.name = name;
}
public Integer getPrice() {
return price;
}
@XmlElement(name = "price")
public void setPrice(Integer price) {
this.price = price;
}
And this is the relevant part of my helper Class, which is planned to store multiple quotes in a set:
import javax.xml.bind.JAXB;
...
public class QuoteService {
Set<Quote> quotes = new LinkedHashSet<>();
public void initializeQuotes() {
String s = "http://finance.yahoo.com/webservice/v1/symbols/aapl/quote?format=xml&view=detail";
try {
URL url = new URL(s);
Quote quote = JAXB.unmarshal(url, Quote.class);
quotes.add(quote);
}
catch (MalformedURLException e) {
e.printStackTrace();
}
public Set<Quote> getQuotes() {
return quotes;
}
}
After executing initializeQuotes(), I do not get any exceptions. However, after printing everything out with the following code:
System.out.println(quoteService.getQuotes());
for (Quote quote : quoteService.getQuotes()) {
System.out.println(quote.getName());
System.out.println(quote.getPrice());
}
I get:
[quote.core.api.Quote@e36bb2a]
null
null
So, obviously, a Quote Class is created, but its attributes (name and price) are empty instead of containing the corresponding XML values ("Apple Inc." and the current price). What am I doing wrong?
If I go to the URL in your code, the XML that comes back is for a "list" object that contains a "resources" object that contains a list of "resource" objects. That doesn't even remotely match your Java object "Quote" class.
You are much better off starting with a schema that describes the XML you want to convert, and then run that schema through xjc, which is the XML to Java compiler. That will generate for you the appropriate annotated java object that you can pass to JAXB marshaller/unmarshaller. (I know the Netbeans IDE will automate for you the step running xjc; other IDEs will probably do that too.) If you want, you can then start customizing the java classes that it generates - but you'll lose all that if later the schema changes and you have to regenerate the java code.
Now in your case, you are starting with XML, perhaps you don't have a schema. But there's a way around that. Go to http://www.freeformatter.com/xsd-generator.html and paste in the XML from the Yahoo site. That will generate for you an XML schema that matches the XML you are getting. That should get you going.
I ran through this for the URL you posted, and it worked on the first try. I didn't actually test unmarshalling the results, but it should work fine.