使用javascript从URL获取XML标记

时间:2015-06-16 15:25:32

标签: javascript ajax xml

我正在尝试从位于此处的下一个xml文件中获取一些标记: http://open.api.ebay.com/shopping?version=581&appid=EBAYU91VTR1SQ8H917BC11O3BW8U4J&action=getResults&callname=FindItemsAdvanced&QueryKeywords=Aston+Martin+V12+Vanquish&categoryId=220

为了从下面的标签中获取“110”,我需要做什么:

<TotalItems>110</TotalItems>

使用javascript?

1 个答案:

答案 0 :(得分:0)

以下是一个基本示例,它使用YQL来绕过CORS

$list = Get-Content "\\server\c$\temp\list.txt"
$storeTestServers = Get-Content "\\server\c$\temp\testStores.txt"

foreach ($server in $storeTestServers) {

    Get-WmiObject -Class Win32_Product -ComputerName $server |
    Select-Object -Property PSComputerName, Name, Version |
    Where-Object {$_.pscomputername -like "940*" -and $_.name -like -in "*$list*"}
}
var pre = document.getElementById('out'),
    oReq = new XMLHttpRequest(),
    url = 'http://open.api.ebay.com/shopping?version=581&appid=EBAYU91VTR1SQ8H917BC11O3BW8U4J&action=getResults&callname=FindItemsAdvanced&QueryKeywords=Aston+Martin+V12+Vanquish&categoryId=220',
    yql = 'http://query.yahooapis.com/v1/public/yql?q=' + encodeURIComponent('select * from xml where url="' + url + '"') + '&format=xml&callback';

function updateProgress(oEvent) {
    if (oEvent.lengthComputable) {
        pre.textContent = oEvent.loaded / oEvent.total;
    } else {
        pre.textContent = 'Unable to compute progress information since the total size is unknown.';
    }
}

function transferComplete(evt) {
    pre.textContent = 'The transfer is complete.';
    
    var doc = new DOMParser().parseFromString(evt.target.responseText, "application/xml"),
        firstTotalItems = doc.getElementsByTagName('TotalItems')[0];
    
    pre.textContent += '\nTotalItems: ' + firstTotalItems.textContent;
}

function transferFailed(evt) {
    status.textContent = 'An error occurred while transferring the file.';
}

function transferCanceled(evt) {
    status.textContent = 'The transfer has been canceled by the user.';
}

oReq.addEventListener("progress", updateProgress, false);
oReq.addEventListener("load", transferComplete, false);
oReq.addEventListener("error", transferFailed, false);
oReq.addEventListener("abort", transferCanceled, false);
oReq.open("GET", yql, true);
oReq.send(null);