-
Combining object array and accessing object property name
I have an xml say in following format
PHP Code:
<color>
<colorName>red,blue,green</colorName>
<hexValue>0xffffff,0x0000,0xcdeff</hexValue>
</color>
I load the xml and parse it in following manner
PHP Code:
for(var i:int =0;i<xmlList.length();i++)
{
var nodeName:String = xmlList[i].name();
//here datalength =3
var nodeArray:Array = nodeData.split(",",dataLength);
//now I loop through this nodearray to creat an object array
for(var j:int =0;j<nodeArray.length;j++)
{
dataArray.push({nodeName:nodeArray[j]});
}
//now I push this dataArray into my final Array
mainArray.push(dataArray);
}
Now what I want to do is create an array in following format based upon above code
PHP Code:
myArray.push({colorName:value,hexValue:value});
// i.e. it should store values in following format
myArray[0].colorName = red;
myArray[0].hexValue =oxfffff;
// here i should dynamically get colorName as object property and so its value
What I should do is parse an xml and from its node name create object property and then create an object array based on those property.
Am I able to make myself clear.
I am still looking for solution and if I find I will post here.I am submitting this query if anyone has certain idea on how to implemnt it[
-
Your parser has many problems. Ignoring the fact that nodeData is undefined (I think you wanted xmlList[i].toString()), you are pushing a new object into dataArray for each value. And you're pushing an object which always has a nodeName property instead of a property named whatever nodeName's value is.
Should the final array look like:
Code:
[{colorName:"red", hexValue:0xffffff}, {colorName:"blue", hexValue:0x0000}, {colorName:"green", hexValue:0xcdeff}]
?
In that case, you'd be better off redesigning your xml to have the colors and hexValues actually associated. But, assuming you know that there are the same number, you could do it in a two pass kind of way.
Code:
var dataArray:Array = [];
var nodeArrays:Array = [];
var nodeNames:Array = [];
var i:int = 0;
for (i = 0; i < xmlList.length; i++){
nodeNames.push(xmlList[i].name());
nodeArrays.push(xmlList[i].toString().split(","));
}
var arrayLength:int = nodeArrays[0].length; //assumes at least one!
for (i = 0; i < arrayLength; i++){
var newObj:Object = {};
for (var j:int = 0; j < nodeNames.length; j++){
newObj[nodeNames[j]] = nodeArrays[j][i];
}
dataArray.push(newObj);
}
This is actually a pretty terrible design, but that's because you've encoded your xml in just about the least friendly way possible. You essentially have to pivot the whole thing from column-centric to row-centric.
If your xml had been more like this, it would be a snap:
Code:
<colors>
<colorValues>
<name>red</name>
<hex>0xff0000</hex>
</colorValues>
<colorValues>
<name>blue</name>
<hex>0x0000ff</hex>
</colorValues>
<colorValues>
<name>green</name>
<hex>0x00ff00</hex>
</colorValues>
</colors>
Incidentally, all your color values don't match up with their names. Some of them don't even have 6 digits.
-
Thanks a ton 5TonsOfFlax
I understood your logic and I am really grateful for your generous snippet.Ok, I implemented the xml in Comma Separated value so that the data transfer and parsing would be easy,but I didn't have any idea on how it turned out to be least data centric.
When I implement the xml as described by your format
PHP Code:
<colors> <colorValues> <name>red</name> <hex>0xff0000</hex> </colorValues> <colorValues> <name>blue</name> <hex>0x0000ff</hex> </colorValues> <colorValues> <name>green</name> <hex>0x00ff00</hex> </colorValues> </colors>
I have this advantage while parsing xml
PHP Code:
var i:uint; var j:uint; var mainArray:Array =[];
for(i =0;i<xmlList.length();i++) { var obj:Object ={}; var nodeList:XMLList = xmlList[i].children(); for(j=0;j<nodeList.length();j++) { var nodeName:* = nodeList[j].name(); var nodeData:* = nodeList[j].toString(); obj[nodeName] = nodeData; } mainArray.push(obj); }
Thank you once again for guiding me and one last question I want to ask you
What is the key to develop more sound programming logic?
Why I am asking you this is I have spent entire day on solution to develop this logic but couldn't do and you simply did it.
How did you achieve this level of logic development?
Patiently waiting for your reply
-
Originally Posted by laaptu
Thank you once again for guiding me and one last question I want to ask you
What is the key to develop more sound programming logic?
Why I am asking you this is I have spent entire day on solution to develop this logic but couldn't do and you simply did it.
How did you achieve this level of logic development?
Patiently waiting for your reply
I would say that the biggest factor is simply experience. I've been programming in some form or other for about 15 years. Eventually you get a feel for some common patterns and how they fit together.
-
Thanks again 5TonsOfFlax now I need more experience on programming.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|