AS3 从 xml 创建多维数组
我想创建一个嵌套数组。这是我加载 xml 后的代码。
function readXML(event:Event):void
{
_data = new XML(event.target.data);
for each (var usr in _data.item)
{
allUserbase.push({name: usr.@name,state: usr.@state,
complex: usr.complex,image:usr.@image, link: usr.@link});
for (var k:int = 0; k< allUserbase.length; k++){
trace(k, allUserbase[k].complex);
for (var t:int = 0; t< allUserbase[k].length; t++){
trace(k,t, allUserbase[k][t]);
// this part when i try to built 2d array doesnt work.. :(*/
}
这是我的 XML:
<content>
<item image="John.jpg" name="John" state = "New Jersey" >
<complex fid = "0"> mg749</complex>
<complex fid = "1"> ks749</complex>
<complex fid = "2"> ks678</complex>
</item>
<item image="Smith.jpg" name="Smith" state = "California">
<complex fid = "0"> we649</complex>
<complex fid = "1"> sd449</complex>
<complex fid = "2"> df459</complex>
<complex fid = "3"> hj569</complex>
</item>
<item image="Smith.jpg" name="Mike" state = "New York">
<complex fid = "0"> 8794</complex>
<complex fid = "1"> 4384</complex>
</item>
</content>;
I want to create a nested arrays. Here is my code, after I loaded xml.
function readXML(event:Event):void
{
_data = new XML(event.target.data);
for each (var usr in _data.item)
{
allUserbase.push({name: usr.@name,state: usr.@state,
complex: usr.complex,image:usr.@image, link: usr.@link});
for (var k:int = 0; k< allUserbase.length; k++){
trace(k, allUserbase[k].complex);
for (var t:int = 0; t< allUserbase[k].length; t++){
trace(k,t, allUserbase[k][t]);
// this part when i try to built 2d array doesnt work.. :(*/
}
Here is my XML:
<content>
<item image="John.jpg" name="John" state = "New Jersey" >
<complex fid = "0"> mg749</complex>
<complex fid = "1"> ks749</complex>
<complex fid = "2"> ks678</complex>
</item>
<item image="Smith.jpg" name="Smith" state = "California">
<complex fid = "0"> we649</complex>
<complex fid = "1"> sd449</complex>
<complex fid = "2"> df459</complex>
<complex fid = "3"> hj569</complex>
</item>
<item image="Smith.jpg" name="Mike" state = "New York">
<complex fid = "0"> 8794</complex>
<complex fid = "1"> 4384</complex>
</item>
</content>;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下内容符合您的要求吗?:
[UPDATE]
您还可以使用
Array
对象和Dictionary
对象的组合,例如下列的:Is the following along the lines of what you want?:
[UPDATE]
You can also use a combination of
Array
objects andDictionary
objects like the following:{名称:usr.@name,状态:usr.@state,
不是一个complex: usr.complex,image:usr.@image, link: usr.@link}
Array
,而是一个Object
声明,它的元素可以通过allUserbase[k].complex
或allUserbase[k].['complex']
访问,它们没有数字索引。{name: usr.@name,state: usr.@state,
is not ancomplex: usr.complex,image:usr.@image, link: usr.@link}
Array
, but anObject
declaration, its elements can be accessed byallUserbase[k].complex
orallUserbase[k].['complex']
, they don't have a numeric index.