无法让 JAXB 解析来自 Yahoo! 的 XML资助 Java
Can't get JAXB to parse XML from Yahoo! Finance to Java
我正在尝试弄清楚如何在 Eclipse 中将以下 XML 解析为 Java class:
http://finance.yahoo.com/webservice/v1/symbols/aapl/quote?format=xml&view=detail
我在 this tutorial 之后尝试过它,但我无法让它工作。
我想要解析名称 (Apple Inc.) 和价格。
这是我的引用 Class(尝试使用和不使用注释参数):
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;
}
这是我的助手 Class 的相关部分,它计划将多个引号存储在一个集合中:
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;
}
}
执行initializeQuotes() 后,我没有得到任何异常。但是,在使用以下代码打印出所有内容后:
System.out.println(quoteService.getQuotes());
for (Quote quote : quoteService.getQuotes()) {
System.out.println(quote.getName());
System.out.println(quote.getPrice());
}
我得到:
[quote.core.api.Quote@e36bb2a]
null
null
因此,很明显,创建了一个报价 Class,但其属性(名称和价格)为空,而不是包含相应的 XML 值(“Apple Inc.”和当前价格).
我做错了什么?
如果我转到您代码中的 URL,返回的 XML 是针对包含 "resources" 对象的 "list" 对象,该对象包含一个列表"resource" 个对象。这甚至不匹配你的 Java 对象 "Quote" class.
最好从描述要转换的 XML 的模式开始,然后通过 xjc 运行 该模式,即 XML 到 Java 编译器。这将为您生成适当的注释 java 对象,您可以将其传递给 JAXB marshaller/unmarshaller。 (我知道 Netbeans IDE 会为您自动执行步骤 运行ning xjc;其他 IDE 可能也会这样做。)如果您愿意,您可以开始自定义 java class 是它生成的 - 但如果以后模式发生变化,你将失去所有这些,你必须重新生成 java 代码。
现在您的情况是从 XML 开始,也许您没有架构。但有办法解决这个问题。转到 http://www.freeformatter.com/xsd-generator.html 并从 Yahoo 站点粘贴到 XML。这将为您生成一个 XML 模式,该模式与您获得的 XML 相匹配。那应该让你去。
我 运行 为您发布的 URL 完成了此操作,并且在第一次尝试时就成功了。我实际上并没有测试解组结果,但它应该可以正常工作。
我正在尝试弄清楚如何在 Eclipse 中将以下 XML 解析为 Java class: http://finance.yahoo.com/webservice/v1/symbols/aapl/quote?format=xml&view=detail
我在 this tutorial 之后尝试过它,但我无法让它工作。
我想要解析名称 (Apple Inc.) 和价格。 这是我的引用 Class(尝试使用和不使用注释参数):
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;
}
这是我的助手 Class 的相关部分,它计划将多个引号存储在一个集合中:
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;
}
}
执行initializeQuotes() 后,我没有得到任何异常。但是,在使用以下代码打印出所有内容后:
System.out.println(quoteService.getQuotes());
for (Quote quote : quoteService.getQuotes()) {
System.out.println(quote.getName());
System.out.println(quote.getPrice());
}
我得到:
[quote.core.api.Quote@e36bb2a]
null
null
因此,很明显,创建了一个报价 Class,但其属性(名称和价格)为空,而不是包含相应的 XML 值(“Apple Inc.”和当前价格). 我做错了什么?
如果我转到您代码中的 URL,返回的 XML 是针对包含 "resources" 对象的 "list" 对象,该对象包含一个列表"resource" 个对象。这甚至不匹配你的 Java 对象 "Quote" class.
最好从描述要转换的 XML 的模式开始,然后通过 xjc 运行 该模式,即 XML 到 Java 编译器。这将为您生成适当的注释 java 对象,您可以将其传递给 JAXB marshaller/unmarshaller。 (我知道 Netbeans IDE 会为您自动执行步骤 运行ning xjc;其他 IDE 可能也会这样做。)如果您愿意,您可以开始自定义 java class 是它生成的 - 但如果以后模式发生变化,你将失去所有这些,你必须重新生成 java 代码。
现在您的情况是从 XML 开始,也许您没有架构。但有办法解决这个问题。转到 http://www.freeformatter.com/xsd-generator.html 并从 Yahoo 站点粘贴到 XML。这将为您生成一个 XML 模式,该模式与您获得的 XML 相匹配。那应该让你去。
我 运行 为您发布的 URL 完成了此操作,并且在第一次尝试时就成功了。我实际上并没有测试解组结果,但它应该可以正常工作。