用Java解析XML文档YQL查询 [英] Parsing XML document YQL query in Java

查看:152
本文介绍了用Java解析XML文档YQL查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用来自使用YQL生成的库存历史数据的XML响应信息,例如此链接

I want to use the information from XML response produced by using YQL for stock historical data, like this link

http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.historicaldata%20where%20symbol%20in%20(%22MSFT%22)%20and%20startDate%3D%222011-2-12%22%20and%20endDate%3D%222011-2-15%22%0A%09%09&diagnostics=true&env=http%3A%2F%2Fdatatables.org%2Falltables.env

并将其存储到股票对象数组中。我是Java的新手,我不了解它的XML api。我不知道这样做的简单方法是什么。有人可以建议我一个好的解决方案。谢谢。

And store it into a stock objects array. I am new to Java and I have no knowledge of its XML api's. I dont know what is the simple way to do it. Can someone suggest me a good solution. Thanks.

推荐答案

您可以使用JAXB执行以下操作( JSR-222 )实施:

You could do the following using a JAXB (JSR-222) implementation:

  • Metro JAXB (the reference implementation included in Java SE 6)
  • EclipseLink JAXB (MOXy), I'm the tech lead
  • Apache JaxMe
  • etc.

演示

import java.io.InputStream;
import java.net.URL;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Stock.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        URL url = new URL("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.historicaldata%20where%20symbol%20in%20(%22MSFT%22)%20and%20startDate%3D%222011-2-12%22%20and%20endDate%3D%222011-2-15%22%0A%09%09&diagnostics=true&env=http%3A%2F%2Fdatatables.org%2Falltables.env");
        InputStream xmlStream = url.openStream();
        Stock stock = (Stock) unmarshaller.unmarshal(xmlStream);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(stock, System.out);
    }

}

股票

import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="query")
@XmlAccessorType(XmlAccessType.FIELD)
public class Stock {

    @XmlElementWrapper(name="results")
    @XmlElement(name="quote")
    private List<Quote> quotes;

}

报价

import java.util.Date;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlSchemaType;

@XmlAccessorType(XmlAccessType.FIELD)
public class Quote {

    @XmlElement(name="Date")
    @XmlSchemaType(name="date")
    private Date date;

    @XmlElement(name="Open")
    private double open;

    @XmlElement(name="High")
    private double high;

    @XmlElement(name="Low")
    private double low;

    @XmlElement(name="Close")
    private double close;

    @XmlElement(name="Volume")
    private long volume;

    @XmlElement(name="Adj_Close")
    private double adjClose;

}

这篇关于用Java解析XML文档YQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆