zok1995 zok1995 - 3 months ago 7
jQuery Question

How can i parse that xml file with jquery

I want to parse tha file, but value "name" in code always have a empty string.

My xml:

<row>
<id>1</id>
<AnrufenZahl>64</AnrufenZahl>
<NameOperator>Ioan</NameOperator>
</row>
<row>
<id>2</id>
<AnrufenZahl>35</AnrufenZahl>
<NameOperator>Dian</NameOperator>
</row>
<row>
<id>3</id>
<AnrufenZahl>50</AnrufenZahl>
<NameOperator>Bob</NameOperator>
</row>


and my JS file
I don`t have output from "alert" function.

$(document).ready(function () { // load xml file using jquery ajax
$.ajax({
type: "GET",
url: "m.xml",
dataType: "xml",
success: function(xml) {
// var output = '<ul>';
$(xml).find('row').each(function(){
$(this).find("NameOperator").each(function(){
var name = $(this).text();
alert(name);
});
});
}
});
});

Answer

Note, the issue is xml is invalid. Adjust to

<?xml version="1.0" encoding="UTF-8"?>
<rows>
<row>
  <id>1</id>
  <AnrufenZahl>64</AnrufenZahl>
  <NameOperator>Ioan</NameOperator>
</row>
<row>
  <id>2</id>
  <AnrufenZahl>35</AnrufenZahl>
  <NameOperator>Dian</NameOperator>
</row>
<row>
  <id>3</id>
  <AnrufenZahl>50</AnrufenZahl>
  <NameOperator>Bob</NameOperator>
</row>
</rows>

then use xml.documentElement

$(xml.documentElement).find(..)

plnkr http://plnkr.co/edit/IUoarUoL7WyDAldZi4Gp?p=preview