PDA

View Full Version : Cannot retrieve XML attribute with Ext.DomQuery



iryndin
16 Feb 2011, 12:27 PM
Hi, guys!
I have following problem: cannot retrieve attribute from XML node with Ext.DomQuery!

I receive following XML from server:


<response status="ok" message="">
<users>
<user id="54">
<sys_role_id>1</sys_role_id>
<title>null</title>
<first_name>Suman</first_name>
<middle_name>null</middle_name>
<last_name>K</last_name>
<email>[email protected]</email>
<phone>1231231234</phone>
<phone_ext>123</phone_ext>
<cell>1231231234</cell>
<comments>test</comments>
<password_retries>0</password_retries>
<is_update_password>false</is_update_password>
<is_locked>false</is_locked>
<date_last_login>null</date_last_login>
<alerts>147</alerts>
</user>
</users>
</response>

and then I want to retrive next info: first name, email. userId.

I do it with following code (this is success handler in Ext.Ajax.request):



success: function(response, options) {
var xml = response.responseXML;
if (Ext.DomQuery.selectNode('error', xml)) {
// report error here ....
} else if (Ext.DomQuery.selectNode('response[status=ok]', xml)) {
// response is OK, retrieve data
var firstName = Ext.DomQuery.selectValue('first_name', xml);
var email = Ext.DomQuery.selectValue('email', xml);
var userId = Ext.DomQuery.selectValue('user/@id', xml);
//console.log(userId);
// do other stuff
}
},


So, this line of code:


var userId = Ext.DomQuery.selectValue('user/@id', xml);

set userId=undefined.

How can I retrieve 'id' attribute from <user> node?

Guys,

thank you in advance for your help!

Regards,
Ivan.

fay
17 Feb 2011, 2:01 AM
I just tried a simplified version of that and it works fine for me.

test.aspx



<%
Response.ContentType = "text/xml"
Response.Write("<response status='ok' message=''><users><user id='54'><first_name>test</first_name></user></users></response>")
%>


test.js



Ext.onReady(function() {
Ext.Ajax.request({
url: './scripts/test.aspx',
success: function(response)
{
var xml = response.responseXML;
console.log(Ext.DomQuery.selectValue('user/@id', xml)); // displays 54!
}
});
});


You can also try:



var userNode = Ext.DomQuery.selectNode('user', xml);
console.log(userNode.attributes.getNamedItem('id').value);


(Note, I only checked this in Firefox.)

landed
2 Apr 2012, 1:48 AM
Trying to use the syntax E[foo] has an attribute "foo"

this was not working so had to get the attribute as in the previous post. Thanks for sharing your findings guys and Sencha please take a look !>:)