为 YQL 查询传递 Raw XML
Pass Raw XML for YQL query
我希望将我自己的 xml 传递给 YQL 以解析为 JSON,而不是让 Yahoo 的服务器查询我提供的 URL 以获得 XML .
Yahoo 查询我提供的页面以获得 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
})
为了避免yahoo 向服务器发出请求。
关于如何做到这一点的任何想法?谢谢
我认为使用内置的 YQL tables(例如 xml
)是不可能的。
但是,这可能是通过使用 custom Open Data Table.
自定义数据table
您可以创建自定义 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
示例查询
现在有了自定义 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
})
链接
- Open Data Tables Reference
- Executing JavaScript in Open Data Tables
- 奖励 link:YQL Editor 你可以 edit/save 自己的自定义数据 tables(我在上面使用了 GitHub,但你没有必须)。
我希望将我自己的 xml 传递给 YQL 以解析为 JSON,而不是让 Yahoo 的服务器查询我提供的 URL 以获得 XML .
Yahoo 查询我提供的页面以获得 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
})
为了避免yahoo 向服务器发出请求。 关于如何做到这一点的任何想法?谢谢
我认为使用内置的 YQL tables(例如 xml
)是不可能的。
但是,这可能是通过使用 custom Open Data Table.
自定义数据table
您可以创建自定义 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
示例查询
现在有了自定义 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
})
链接
- Open Data Tables Reference
- Executing JavaScript in Open Data Tables
- 奖励 link:YQL Editor 你可以 edit/save 自己的自定义数据 tables(我在上面使用了 GitHub,但你没有必须)。