1

This is an example of my xml file, with <studentenhuizen> repeated multiple times:

<studentenhuis>
  <studentenhuizen>
    <adres>Aalbeeksesteenweg</adres>
    <huisnr>19</huisnr>
    <gemeente>KORTRIJK</gemeente>
    <aantal_kamers>14</aantal_kamers>
  </studentenhuizen>
</studentenhuis>

My object

function StudentenKot(adres, huisnr, gemeente, aantalkamers){
    this.adres = adres;
    this.gemeente = gemeente;
    this.huisnr = huisnr;
    this.aantalSlaapkamers = aantalkamers;
};

Load xml file:

$.ajax({
    type: "GET",
    dataType: "xml",
    url:url,
    success: function (xml) {
        studentenhuis = new Array();
        $(xml).find("studentenhuizen").each(function () {
            studentenhuis.push(new StudentenKot(this.adres, this.huisnr, this.gemeente, this.aantal_kamers));
        });

        $.each(studentenhuis, function (i) {
            $(".studentenkoten").append("<div class='gemeente'>" + studentenhuis[i].adres + "</div>");
        });
    }
});

When added to the <div class="gemeente"> it says "undefined". This worked before, but it says [Object object] now

alert($(xml).find("adres")); 
4
  • What do you this.adres, this.huisnr, … expect to be in that each loop? this is an XML node, isn't it? Commented Jan 19, 2014 at 21:48
  • 1
    Yeah it is, I confused it with json haven't I? Either way 'adres = $(xml).find("adres")' --> alert(adres) won't work either. Commented Jan 19, 2014 at 21:50
  • Try console.log(studentenhuis[i]) in your last each loop to see the structure of it. Chances are you're just missing a key identifier or something trivial like that Commented Jan 19, 2014 at 21:55
  • 1
    It says: aantalSlaapkamers: undefined adres: undefined gemeente: undefined huisnr: undefined __proto__: Object Commented Jan 19, 2014 at 22:02

1 Answer 1

1

this.adres - I confused it with json haven't I?

Yes, you have. This should've said undefined when alerting/stringifying it.

$(xml).find("adres") - it says [Object object] now

Yes, what find returns is a jQuery collection object which will get stringified to "[object Object]". You want:

  • not the <adres> node (nor a jQuery collection with it), but its text content
  • to search the current <studentenhuizen> node, not the whole xml.

So use

$(xml).find("studentenhuizen").each(function () {
    studentenhuis.push(new StudentenKot(
        $(this).find("adres").text(),
        $(this).find("huisnr").text(),
        $(this).find("gemeente").text(),
        $(this).find("aantal_kamers").text()
     ));
});
Sign up to request clarification or add additional context in comments.

3 Comments

Didn't know that jQuery can wrap XML, too. :)
@Peter: Wrapping is no problem, only some html-specific methods (.html(), offset(), animate() etc) might not work. However, the way jQuery does parse HTML was not designed to work with XML, so there are lots of cross-browser problems with this (1). I personally wouldn't use it, but that's out of the scope of this question.
Okay, there are seem to be pretty many issues with this kind of approach. Haven't tried this one : xmljs.sourceforge.net yet, but it looks pretty complete.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.