1

我通过 YQL 获取数据。我到达使用 java 解析的答案来自Parsing XML document YQL query in Java for a request yahoo.finance.quotes

但是当请求 google.translate 时,我遇到了 Java 解析器结果的问题。

感谢您帮助我完成脚本工作。我有以下课程。

import java.io.InputStream;
import java.net.URL;
import java.net.URLEncoder;
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();

        String text = "this is a test";
        String target = "de";

        String root = "http://query.yahooapis.com/v1/public/yql?env=http%3A%2F%2Fdatatables.org%2Falltables.env&format=json&q=";
        String yql = "select * from google.translate where q='"+text+"'"+"and target='"+target+"'";
        String yqlquerytranslation = "http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20google.translate%20where%20q%3D%22"+text+"%22%20and%20target%3D%22"+target+"%22%3B&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys";

        //URL url = new URL("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22YHOO%22%2C%22GOOG%22)&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys");
        URL url = new URL(root+URLEncoder.encode(yql));
        InputStream xmlStream = url.openStream();
        System.out.println(xmlStream);
        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 Json {


    @XmlElement(name="json")
    private String json;

}
4

0 回答 0