2017-08-13 72 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 
}) 

鏈接