XML > jQuery reading

雨燕双飞 提交于 2019-12-18 07:22:57

问题


How can i read this XML File with jQuery? With "normal tags" its no problem like:<car>Mustang</car>

HTML/jQuery:

$(document).ready(function(){
$.get("AMA.xml", function(XMLArray){
$(XMLArray).find("dataset").each(function(){

var $myAMA = $(this);
var number = $myAMA.attr("article.plunumber");
var name = $myAMA.attr("article.name");
var price = $myAMA.attr("article.price").text();
$("#AMAContainer").append("<p>"+number+"<br>"+name+"<br>"+price+"</p>");
  });
 });
});

XML File:

<document name="screen">
   <section name="list">
        <dataset>
            <var key="type">article</var>
            <var key="article.number">1</var>
            <var key="article.name">Testname1</var>
            <var key="article.displayname"/>
            <var key="article.price">99,99</var>
            <var key="article.plunumber">1</var>
            <action key="STANDARD">bp1;</action>
        </dataset>
        <dataset>
            <var key="type">article</var>
            <var key="article.number">2</var>
            <var key="article.name">Testname2</var>
            <var key="article.price">88,88</var>
            <var key="article.plunumber">2</var>
            <action key="STANDARD">bp2;</action>
        </dataset>
    </section>
</document>

I have absolute no idea how to read the values. I only need the three(number,name,price).


回答1:


You can select elements by attribute, you just have to use an attributes selector, or a filter() function etc.

$(document).ready(function () {
    $.get("AMA.xml", function (XMLArray) {
        var xml = $.parseXML(XMLArray);
        $(xml).find("dataset").each(function () {
            var number = $('[key="article.plunumber"]', this).text();
            var name = $('[key="article.name"]', this).text();
            var price = $('[key="article.price"]', this).text();
            $("#AMAContainer").append("<p>" + number + "<br>" + name + "<br>" + price + "</p>");
        });
    });
});



回答2:


var number = $myAMA.find('var[key="article.plunumber"]');

Treat the <var> just like you're treating the <dataset>



来源:https://stackoverflow.com/questions/14782408/xml-jquery-reading

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!