简体   繁体   中英

How can I log a specific child in some XML data?

$.get('MyPHPFileThatDownloadsAndPrintsTheXML.php', function(data) {
    console.log(data.childNodes[0]);
}, 'xml');

Let's say I want to log the first row's stageName. How would I go about doing this with the code I already have?

Thanks

You can traverse the XML like you would HTML with jQuery DOM methods :

$.get('MyPHPFileThatDownloadsAndPrintsTheXML.php', function(data) {
    var txt =  $(data).find('row').first().find('stageName').text();
    console.log(txt);
}, 'xml');

Using pure JavaScript to get XML entries is the same as navigating any #document using DOM-only methods. The following example gets the 14th row's stageName (14th = index 14 - 1 === 13 )

function (data) {
    function getData(rowIndex, key, index) {
        return data[rowIndex].getElementsByTagName(key)[index || 0];
    }
    function getTextData(rowIndex, key, index) {
        return getData(rowIndex, key, index).textContent;
    }
    console.log(getTextData(13, 'stageName'));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM