【问题标题】:Jquery Parsing an XML file outputs a blank tableJquery解析XML文件输出一个空白表
【发布时间】:2011-12-28 21:59:55
【问题描述】:

我正在尝试通过使用 jquery 解析 XML 文件来创建表。 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...

【问题讨论】:

  • 如果我在 $('TextLine1',xml).each(function(i){ 我不会收到任何输出。但如果我把它放在那行之前,我会看到输出。

标签: javascript jquery xml parsing


【解决方案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/

【讨论】:

  • 感谢您的精彩资源和回答!
猜你喜欢
  • 2023-03-23
  • 1970-01-01
  • 1970-01-01
  • 2014-03-01
  • 2011-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多