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

前端 未结 2 2002
没有蜡笔的小新
没有蜡笔的小新 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:39

    The right solution is shown as answer at this question

    Parse xml with namespaces using JQuery and working for all browser ..

    It works on IE, FF and Chrome now!

    I hope this could be useful for others.

    Cesare

    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题