开发者

Parse XML NaN Error?

开发者 https://www.devze.com 2023-03-30 08:42 出处:网络
i got this code with JS, parsing from XML, my Output displays NaN in Chrome, FF, IE... and I don\'t know开发者_如何学Go why, wether where it comes from. All my XML fields are Chars, thats why i use .t

i got this code with JS, parsing from XML, my Output displays NaN in Chrome, FF, IE... and I don't know开发者_如何学Go why, wether where it comes from. All my XML fields are Chars, thats why i use .text() function...

    function parse(document){
    $(document).find("EMaDetails").each(function(){
        $("#main").append(
            '<table>'
            +'<tr>'+'<td>'
            +$(this).find('Nachn').text()+', '
            +$(this).find('Vorna').text()
            +'</td>'+'</tr>'+
            +'<tr>'+'<td>'
            +$(this).find('Detail1').text()+', '
            +$(this).find('Detail2').text()
            +'</td>'+'</tr>'
            +'</table>'
            );


    });
}

And the Result is like this:

NaN
Lastname1, Firstname1
Detail1, Detaila1
NaN
Lastname2, Firstname2
Detail2, Detaila2

Thanks


+'</td>'+'</tr>'+

remove the + at the and of this line(the next line starts with a + too)


(This not an not answer but more of a comment, however I needed more space and the formatting of an answer to show my point.)

The best way to avoid this kind of error is to consistently use a code convention on how to format your code. There are many suggestions for code conventions around, but it's not important which one you use, as long you are comfortable with it and most importantly use it consistently.

In you case, where you are wrapping a long expression over several lines, there are three thing you should look out for:

  • Ident the following lines
  • Use spaces before and after a binary operator
  • And most importantly place the operators consistently either at the start or the end of the line. (Don't go mixing the placement, or you'll get errors like you did.)

Personnally I like to have the operator at the end of the line, so you "know" that the line has to continue, and there is less danger to confuse the binary + with a unary + as it happened in your case.

'<table>' +
  '<tr>' + '<td>' +
  $(this).find('Nachn').text() + ', ' +
  $(this).find('Vorna').text() +
  '</td>' + '</tr>' +
  '<tr>' + '<td>' +
  $(this).find('Detail1').text() + ', '  +
  $(this).find('Detail2').text()  +
  '</td>' + '</tr>' +
  '</table>'
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号