1

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).

tckmn
  • 57,719
  • 27
  • 114
  • 156
TheCyX
  • 23
  • 1
  • 1
  • 6
  • Off-topic, but this plug-in may simplify working with XML, it will convert an XML to a JSON object, which is often easier to handle in javascript http://www.fyneworks.com/jquery/xml-to-json/ some examples can be found here http://stackoverflow.com/questions/3642789/how-to-convert-xml-to-json-using-jquery – thaJeztah Feb 08 '13 at 23:06

2 Answers2

3

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>");
        });
    });
});
adeneo
  • 312,895
  • 29
  • 395
  • 388
  • Firefox says: "TypeError: $.parseXML is not a function" and page is still blank EDIT: updated jquery to 1.9.1 and now, no alert in FF anymore, page is still blank :( – TheCyX Feb 08 '13 at 23:00
  • Still blank, dont know why.. [at]idrumgood: values are shown as: [object Object] | Thanks!! – TheCyX Feb 08 '13 at 23:17
  • @HolgerFette - Yes, the number and name variables are DOM elements, so that comes out as [object, object]. If you need the elements content, you'll need to use `text()` or something similar as well, like you do on the last one, `price` !! Changed the code to that ? – adeneo Feb 08 '13 at 23:37
0
var number = $myAMA.find('var[key="article.plunumber"]');

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

idrumgood
  • 4,904
  • 19
  • 29