How to parse xml with namespaces using JQuery (and working for all browser .. )?

前端 未结 2 2004
没有蜡笔的小新
没有蜡笔的小新 2021-01-20 01:58

I need to parse an XML response from a web service using JQuery

http://code.jquery.com/jquery-1.11.0.min.js

Here you are a sample of my XM

2条回答
  •  南方客
    南方客 (楼主)
    2021-01-20 02:52

    You can iterate through the XML elements using jQuery and find(), just like with HTML. When specifying tag names to select, you need to omit the namespace prefix in the selector.

    var xmlText = $('#featureData').text(),
        $xmlData = $.parseXML(xmlText),
        $features = $('featureMember', $xmlData),
        extractedFeatures = [];
    
    $features.each(function () {
        var $this = $(this),
            feature = {},
            items = [
                'nome',
                'civico',
                'istat',
                'cap',
                'comune'
            ],
            item;
    
        for (var i = 0; i < items.length; i++) {
            item = items[i];
            feature[item] = $this.find(item).text();
        }
    
        extractedFeatures.push(feature);
    });
    
    $('#output').text(JSON.stringify(extractedFeatures));
    

    See the jsFiddle reproduction here

提交回复
热议问题