2011-12-28 86 views
1

我想通過解析一個XML文件與jQuery創建一個表。 XML文件看起來像這樣...Jquery解析XML文件輸出一個空白表

<?xml version='1.0' encoding='UTF-16'?> 
<E1TC NAME='R584211WXB' VERSION='a'> 
<TextLine1> 
    <1>2011/11/06</1> 
    <2>5748283</2> 
    <3>10.9300</3> 
    <4>4049027</4> 
    <5>7.7000</5> 
    <6>42799422</6> 
    <7>81.3800</7> 
    <8>52596733</8> 
</TextLine1> 
<TextLine1> 
... 
</TextLine1> 
</E1TC> 

我jQuery的功能看起來像這樣

 // Build an HTML string 
     myHTMLOutput = ''; 
     myHTMLOutput += '<table width="98%" border="1" cellpadding="0" cellspacing="0">'; 
     myHTMLOutput += '<th>1</th><th>2</th><th>3</th><th>4</th><th>5</th><th>6</th><th>7</th><th>8</th>'; 

     // Run the function for each TextLine1 tag in the XML file 
     $('TextLine1',xml).each(function(i) 
     { 
      d1 = $(this).find("1").text(); 
      d2 = $(this).find("2").text(); 
      d3 = $(this).find("3").text(); 
      d4 = $(this).find("4").text(); 
      d5 = $(this).find("5").text(); 
      d6 = $(this).find("6").text(); 
      d7 = $(this).find("7").text(); 
      d8 = $(this).find("8").text(); 
      d1Post = $(this).find("1").attr("post"); 

      // Build row HTML data and store in string 
      mydata = BuildStudentHTML(d1,d2,d3,d4,d5,d6,d7,d8,d1Post); 
      myHTMLOutput = myHTMLOutput + mydata; 
     }); 
     myHTMLOutput += '</table>'; 

輸出端產生一個空白的表頭1,2,3,...,8。我找不到我的錯誤。我知道這個代碼有效,因爲它是來自http://www.compoc.com/tuts/的一個例子。當我修改它來處理我的XML文檔時,一些東西破裂了。它看起來像是有什麼錯在線

$('TextLine1',xml).each(function(i) 

有關如何解決它的任何想法?這是我第一次使用jQuery ...

+0

如果我堅持**警告(「此代碼運行」); **行後 ** $('TextLine1',xml).each(function(i){**我將不會收到輸出,但如果我堅持它在那行之前,我會看到輸出。 – whittin36 2011-12-28 22:06:17

回答

1

看起來你的XML是無效的。問題似乎是你用數字命名標籤。如果我改變你的XML看起來像下面然後正常工作:

<?xml version='1.0' encoding='UTF-16'?> 
<E1TC NAME='R584211WXB' VERSION='a'> 
<TextLine1> 
    <a>2011/11/06</a> 
    <b>5748283</b> 
    <c>10.9300</c> 
    <d>4049027</d> 
    <e>7.7000</e> 
    <f>42799422</f> 
    <g>81.3800</g> 
    <h>52596733</h> 
</TextLine1> 
</E1TC> 

我用這個網站來驗證XML:http://xmlgrid.net/

這裏是上述XML的演示:http://jsfiddle.net/cHA4D/

+0

謝謝你的很好的資源和答案! – whittin36 2011-12-29 14:14:46