2017-08-13 90 views
0

我正在寻找将我自己的xml传递给YQL以解析为JSON,而不是让雅虎的服务器查询我提供的URL来获取XML。通过YQL查询的原始XML

其中雅虎查询我提供给获得JSON响应页面当前请求:

queue_request({ 
    dataType: 'jsonp', 
    url: 'https://query.yahooapis.com/v1/public/yql', 
    data: { 
     q: 'select * from xml where url="' + url + '"', 
     format: 'json' 
    }, 
    complete: callback 
}) 

我想沿(伪代码)行做一些事情:

queue_request({ 
    dataType: 'jsonp', 
    url: 'https://query.yahooapis.com/v1/public/yql', 
    data: { 
     q: 'select * from xml where xmlstring="' + xml + '"', 
     format: 'json' 
    }, 
    complete: callback 
}) 

为了避免雅虎向服务器发出请求。 关于如何做到这一点的任何想法?谢谢。

回答

0

我不认为有可能使用内置的YQL表(例如xml)。 但是,它可能是通过使用custom Open Data Table

自定义数据表

您可以创建自定义表并使用<execute> block to execute some JavaScript来解析查询提供的XML字符串,使生成的XML对象是从查询结果。

... 
<select itemPath="" produces="XML"> 
    <inputs> 
    <key id="xmlstring" type="xs:string" paramType="variable" required="true"/> 
    </inputs> 
    <execute><![CDATA[ 
    response.object = new XML(xmlstring); 
    ]]></execute> 
</select> 
... 

»见the full example data table

例子查询

现在有一个自定义的表格中,我们可以在查询use它。

use "https://raw.githubusercontent.com/salathe/yql-tables/examples/data/xmlstringarg.xml" as xml; 
select * from xml where xmlstring="<example><a>A</a><b>B</b></example>"; 

»见this query in the YQL console

你的代码示例

真的,只是查询本身已更改为包括use声明。

var query = 'use "https://raw.githubusercontent.com/salathe/yql-tables/examples/data/xmlstringarg.xml" as xml;' 
      + 'select * from xml where xmlstring="' + xml + '"'; 
queue_request({ 
    dataType: 'jsonp', 
    url: 'https://query.yahooapis.com/v1/public/yql', 
    data: { 
     q: query, 
     format: 'json' 
    }, 
    complete: callback 
}) 

链接