开发者

Parsing XMLRPC struct with jQuery

开发者 https://www.devze.com 2023-04-03 13:20 出处:网络
I\'m trying to parse an XMLRPC response using jQuery to create a table containing the contents of an array of structs.The response looks like this:

I'm trying to parse an XMLRPC response using jQuery to create a table containing the contents of an array of structs. The response looks like this:

<?xml version="1.0"?>
<methodResponse><params><param><value><array><data>
    <value><struct><member><name>time</name><value>1315415195</value></member><member><name>level</name><value>DEBUG</value></member><member><name>thread</name><value>0x805e558</value></member><member><name>message</name><value>glib_syslog_get_log_messages</value></member></struct></value>
    <value><struct><member><name>time</name><value>1315415160</value></member><member><name>level</name><value>DEBUG</value></member><member><name>thread</name><value>backup-Backups</value></member><member><name>message</name><value>Sleeping 5 minutes</value></member></struct></value>
    ....
</data></array></value></param></params></methodResponse>

I then parse the respo开发者_开发技巧nse in my callback function. The first fine works great and finds all the struct elements in the response. The 2nd find is attempting to find the value of the member with the name time, but instead just returns the struct element again. What is the correct selector for "Find me the text of the value element of the member with the name X?"

function loadStatusDone(data) {
    if(!data) {
        return;
    }

//first, must clean the content viewer
$("table#tableStatus tbody").children().remove();
$(data).find('struct').each(function(){
    var ts = $(this).find("member name:contains(time) :parent value").text();
    var level = "level";
    var thread = "thread";
    var message = "some message";

    var html = "<tr>" +
    "<td>" + ts + "</td>" +
    "<td>" + level + "</td>" +
    "<td>" + thread + "</td>" +
    "<td>" + message + "</td>" +
    "</tr>";


    $("table#tableStatus tbody").append(html);
    $("table").trigger("update");
});
}


You can split up

var ts = $(this).find("member name:contains(time) :parent value").text();

into:

var $mem = $(this).find('member name:contains(time)').parent(),
    ts = $('value', $m).text();

That will select the name node with "time" as the text, then get its parent (which is the containing member node). Then use the member node as the context of where to get the value node and return the text of it. I tested this on your XML and it seems to work for me.

My Test Code

//dirty version of your XML
var $xml = '<methodResponse><params><param><value><array><data><value><struct><member><name>time</name><value>1315415195</value></member><member><name>level</name><value>DEBUG</value></member><member><name>thread</name><value>0x805e558</value></member><member><name>message</name><value>glib_syslog_get_log_messages</value></member></struct></value><value><struct><member><name>time</name><value>1315415160</value></member><member><name>level</name><value>DEBUG</value></member><member><name>thread</name><value>backup-Backups</value></member><member><name>message</name><value>Sleeping 5 minutes</value></member></struct></value></data></array></value></params></methodResponse>';

$xml.find('struct').each(function(){
    var $m = $(this).find('member name:contains(time)').parent(),
        ts = $('value', $m).text();

    console.log(ts);
});
0

精彩评论

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