简体   繁体   中英

XML jquery parse help

I have a weird XML file that I'm trying to parse.

<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>

I tried getting the data by using "Row" which works but how do I call the individual field names? In firebug they all look like "Field". I tried:

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

But that doesn't grab it.

Thanks


Code from a comment below:

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()
    };
}

You can do this with the attribute equals selector [attrname="value"] . For instance:

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

This finds the Field element whose name is whatever2 within the first (index 0 ) Row element.

try $(Field [name=whatever])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM