{ "id": "63806", "key": "TIMOB-3174", "fields": { "issuetype": { "id": "2", "description": "A new feature of the product, which has yet to be developed.", "name": "New Feature", "subtask": false }, "project": { "id": "10153", "key": "TIMOB", "name": "Titanium SDK/CLI", "projectCategory": { "id": "10100", "description": "Titanium and related SDKs used in application development", "name": "Client" } }, "fixVersions": [ { "id": "12087", "description": "", "name": "Sprint 2011-47", "archived": true, "released": true, "releaseDate": "2011-11-28" } ], "resolution": { "id": "2", "description": "The problem described is an issue which will never be fixed.", "name": "Won't Fix" }, "resolutiondate": "2011-11-29T15:45:58.000+0000", "created": "2011-04-15T03:38:49.000+0000", "priority": { "name": "Trivial", "id": "5" }, "labels": [], "versions": [ { "id": "11244", "name": "Release 1.7.0", "archived": true, "released": true, "releaseDate": "2011-06-13" } ], "issuelinks": [], "assignee": { "name": "vduggal", "key": "vduggal", "displayName": "Vishal Duggal", "active": false, "timeZone": "America/Los_Angeles" }, "updated": "2011-11-29T15:45:58.000+0000", "status": { "description": "The issue is considered finished, the resolution is correct. Issues which are closed can be reopened.", "name": "Closed", "id": "6", "statusCategory": { "id": 3, "key": "done", "colorName": "green", "name": "Done" } }, "components": [ { "id": "10206", "name": "iOS", "description": "iOS Platform" } ], "description": "{html}

Problem

\r\n

We can't iterate over attributes in a node. We can only access\r\nthem by their name. We do expose the \"attributes\" property, which\r\ntoStrings as a \"[object TiDOMNamedNodeMap]\". The length property of\r\nattributes correctly returns the number of attributes an element\r\nhas, but there is no way to access an individual attribute by index\r\n(for example, node.attributes[0] is undefined).

\r\n

Solution

\r\n

Fix the attributes property to let you access each attribute by\r\nindex, or expose a function to let you do this. The former approach\r\nwould be more inline with major browsers.

\r\n

Sample Code

\r\n

The following sample code will write 'FAIL: some message' in a\r\nlabel if it isn't able to grab an attribute out of a simple XML\r\nstring. Otherwise, it will write out the attribute name and value\r\nin a happy green color.

\r\n
\r\nvar win = Ti.UI.createWindow({ backgroundColor: '#fff' });\r\nvar label = Ti.UI.createLabel();\r\nwin.add(label);\r\n\r\nlabel.color = '#f00';\r\nlabel.text += 'FAIL: If you have time to read this, then we never finished reading the attributes.';\r\n\r\nvar dDocument = Titanium.XML.parseString('<?xml version=\"1.0\" encoding=\"utf-8\"?>\\r\\n<root foo=\"bar\" />');\r\nvar node = dDocument.documentElement;\r\nlabel.text = 'Found ' + node.attributes.length + ' attributes, ';\r\nfor (var i = 0; i < node.attributes.length; i++) {\r\n    var attribute = node.attributes[i];\r\n    if (attribute) {\r\n        label.color = '#0f0';\r\n        label.text += attribute.name + \" = \" + attribute.value;\r\n    }\r\n    else {\r\n        label.color = '#f00';\r\n        label.text += 'FAIL: attribute undefined.';\r\n    }\r\n}\r\n\r\nwin.open();\r\n
\r\n

Tested On

\r\n

Pulled from GitHub master last night: Titanium SDK version:\r\n1.7.0 (02/17/11 20:28 316c2c7)
\r\nBROKEN on iPhone Simulator 4.2

\r\n

Associated Helpdesk Ticket

\r\n

http://developer.appcelerator.com/helpdesk/view/73221

{html}", "attachment": [], "flagged": false, "summary": "iOS: Iterating Over XML Element Attributes", "creator": { "name": "dtoth", "key": "dtoth", "displayName": "Dawson Toth", "active": true, "timeZone": "America/New_York" }, "subtasks": [], "reporter": { "name": "anagesh", "key": "anagesh", "displayName": "Anirudh Nagesh", "active": true, "timeZone": "America/Los_Angeles" }, "environment": null, "comment": { "comments": [ { "id": "130229", "author": { "name": "jeroenk.", "key": "jeroenk.", "displayName": "Jeroen K.", "active": true, "timeZone": "America/Los_Angeles" }, "body": "{html}

And what about .childNodes ?

{html}", "updateAuthor": { "name": "jeroenk.", "key": "jeroenk.", "displayName": "Jeroen K.", "active": true, "timeZone": "America/Los_Angeles" }, "created": "2011-04-15T03:38:49.000+0000", "updated": "2011-04-15T03:38:49.000+0000" }, { "id": "161005", "author": { "name": "anagesh", "key": "anagesh", "displayName": "Anirudh Nagesh", "active": true, "timeZone": "America/Los_Angeles" }, "body": "Also, we cannot set the attribute value using setAttribute().\r\nh3. Associated helpdesk ticket\r\n\r\nappc.me/c/APP-546582", "updateAuthor": { "name": "anagesh", "key": "anagesh", "displayName": "Anirudh Nagesh", "active": true, "timeZone": "America/Los_Angeles" }, "created": "2011-07-28T11:25:17.000+0000", "updated": "2011-07-28T11:25:17.000+0000" }, { "id": "163617", "author": { "name": "dtoth", "key": "dtoth", "displayName": "Dawson Toth", "active": true, "timeZone": "America/New_York" }, "body": "Changing reporter to Anirudh, as he bumped it to \"Within Release\" on the 19th of August.", "updateAuthor": { "name": "dtoth", "key": "dtoth", "displayName": "Dawson Toth", "active": true, "timeZone": "America/New_York" }, "created": "2011-08-22T09:40:24.000+0000", "updated": "2011-08-22T09:40:24.000+0000" }, { "id": "169479", "author": { "name": "blainhamon", "key": "blainhamon", "displayName": "Blain Hamon", "active": true, "timeZone": "America/Los_Angeles" }, "body": "Changing this to a feature request. Turns out that the w3 DOM 2 spec does not specify usage of the attributes as an array. Furthermore, nodes do not have name nor value attributes. ( http://www.w3.org/TR/2000/REC-DOM-Level-2-Core-20001113/core.html )\r\n\r\nRepeat: attributes[index], node.name, and node.value are all browser extensions and NOT part of the w3 spec. The w3 spec is attributes.item(index), node.nodeName, and node.nodeValue. After fixing the sample code to reflect this.\r\n\r\n{code}\r\nvar win = Ti.UI.createWindow({ backgroundColor: '#fff' });\r\nvar label = Ti.UI.createLabel();\r\nwin.add(label);\r\n\r\nlabel.color = '#f00';\r\nlabel.text += 'FAIL: If you have time to read this, then we never finished reading the attributes.';\r\n\r\nvar dDocument = Titanium.XML.parseString('\\r\\n');\r\nvar node = dDocument.documentElement;\r\nlabel.text = 'Found ' + node.attributes.length + ' attributes, ';\r\nfor (var i = 0; i < node.attributes.length; i++) {\r\n var attribute = node.attributes.item(i);\r\n if (attribute) {\r\n label.color = '#0f0';\r\n label.text += attribute.nodeName + \" = \" + attribute.nodeValue;\r\n }\r\n else {\r\n label.color = '#f00';\r\n label.text += 'FAIL: attribute undefined.';\r\n }\r\n}\r\n\r\nwin.open();\r\n{code}\r\n\r\nThe code ran fine. Changing this to a feature request to allow for the browser extension usage.", "updateAuthor": { "name": "blainhamon", "key": "blainhamon", "displayName": "Blain Hamon", "active": true, "timeZone": "America/Los_Angeles" }, "created": "2011-10-17T14:55:52.000+0000", "updated": "2011-10-17T14:55:52.000+0000" }, { "id": "174228", "author": { "name": "stephentramer", "key": "stephentramer", "displayName": "Stephen Tramer", "active": true, "timeZone": "America/Los_Angeles" }, "body": "Not necessary. Here's the DOM2 IDL definition for NamedNodeMap:\r\n\r\nhttp://www.w3.org/TR/DOM-Level-2-Core/idl-definitions.html\r\n\r\nInstead, this can be done with the \"length\" attribute, and by calling the \"item()\" method to get the item at that index. NamedNodeMap objects should not behave like arrays.", "updateAuthor": { "name": "stephentramer", "key": "stephentramer", "displayName": "Stephen Tramer", "active": true, "timeZone": "America/Los_Angeles" }, "created": "2011-11-29T15:45:58.000+0000", "updated": "2011-11-29T15:45:58.000+0000" } ], "maxResults": 5, "total": 5, "startAt": 0 } } }