yql google.tranlate parse java

leonstrife

New Member
I get data through YQL. and I arrive to parse using java with the answer from Parsing XML document YQL query in Java for a request yahoo.finance.quotesBut when is a request google.translate, I have problems with Java parser result.Thank you for helping me with the script works. I have the following class.\[code\]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); }}\[/code\]second class\[code\]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;}\[/code\]Last class\[code\]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;}\[/code\]
 
Back
Top