XML jquery解析帮助

时间:2011-06-07 17:20:41

标签: javascript jquery xml parsing

我有一个奇怪的XML文件,我正在尝试解析。

<Data>
<Row>
<Field name = "whatever" pos ="1">STUFF</Field>
<Field name = "whatever2" pos ="2">MORE STUFF</Field>
<Field name = "whatever3" pos ="3">EVEN MORE STUFF</Field>
</Row>
<Row>
<Field name = "whatever" pos ="1">Different STUFF</Field>
<Field name = "whatever2" pos ="2">MORE Different STUFF</Field>
<Field name = "whatever3" pos ="3">EVEN MORE Different STUFF</Field>
</Row>
</Data>

我尝试使用“Row”来获取数据但是如何调用各个字段名称?在萤火虫中,它们看起来都像“场”。我试过了:

myvalue : $("whatever", this).text()

但这并没有抓住它。

由于


以下评论中的代码:

function callAjax(url) {
    $.ajax({
        url: url,
        dataType: "xml",
        success: function (xmlResponse) {
            $.merge(data1, $("ROW", xmlResponse).map(returnResults).get());
        } // end of success }); }

function returnResults() {
    formatedURL = $('Field[name="EL_VALUES_FIELD2"]', this).text();
    return {
        value: $('Field[name="EL_VALUES_FIELD1"]', this).text(),
        label: $('Field[name="EL_VALUES_FIELD1"]', this).text() + " " + $("EL_VALUES_FIELD4", this).text(),
        title: $('Field[name="EL_VALUES_FIELD1"]', this).text(),
        url: formatedURL,
        description: $('Field[name="EL_VALUES_FIELD3"]', this).text(),
        keywords: $('Field[name="EL_VALUES_FIELD4"]', this).text()
    };
}

2 个答案:

答案 0 :(得分:1)

您可以使用attribute equals selector [attrname="value"]执行此操作。例如:

$('Row', this).eq(0).find('Field[name="whatever2"]').text()

这会在第一个(索引Fieldwhatever2元素中找到名称为0的{​​{1}}元素。

答案 1 :(得分:0)

尝试$(Field [name=whatever])