|
-
[RESOLVED] XML comma delimited entries to Array
Hi All
I have the following XML
Code:
<shops>
<shop id="01">
<name>Boots</name>
<categories>Cosmetics, Pharmaceuticals, Food</categories>
</shop>
<shop id="02">
<name>JJB Sports</name>
<categories>Sportswear, Football, Rugby, Tennis</categories>
</shop>
</shops>
What I want to be able to do is to extract all of the items listed in the categories and put them into an array as individual items. So would like have an array that contained Cosmetics, Pharmaceuticals, Food, Sportswear, Football, Rugby and Tennis as individual items within the array.
Any help would be GREATLY appreciated.
Thanks
Phil
-
Why would you throw out the structuring abilities that XML gives you, just to poorly re-invent ways to do it?
Make your xml like this:
Code:
<shops>
<shop id="01">
<name>Boots</name>
<categories>
<category>Cosmetics</category>
<category>Pharmaceuticals</category>
<category>Food</category>
</categories>
</shop>
<shop id="02">
<name>JJB Sports</name>
<categories>
<category>Sportswear</category>
<category>Football</category>
<category>Rugby</category>
<category>Tennis</category>
</categories>
</shop>
</shops>
Then you can just get the categories with
Code:
var categories:XMLList = someXMLVariable.shop.categories.category;
If you cannot change the xml, then you can take that string value and split it on ", "
Code:
var categories:Array = categoriesString.split(", ");
Where categoriesString is the String value of the categories node.
-
I agree
It would be much better to have the XML structured the way in which you said, but when you are receiving the data from a client and this is how their database is set up with over 15,000 shops,it would be a slight pain in the a to try and restructure everything.
I already worked out how to split the categories, but I want it to loop through each shop node and extract the categories information and add them to the same array.. any ideas?
-
Code:
var categories:Array = [];
var allCategoriesNodes:XMLList = someXMLVariable.shop.categories;
var categoriesNode:XML;
for each (categoriesNode in allCategoriesNodes){
categories.concat(categoriesNode.toString().split(", "));
}
-
Thanks 5TonsOfFlax.. just what I needed fella.
-
 Originally Posted by PhilJ2008
It would be much better to have the XML structured the way in which you said, but when you are receiving the data from a client and this is how their database is set up with over 15,000 shops,it would be a slight pain in the a to try and restructure everything.
I already worked out how to split the categories, but I want it to loop through each shop node and extract the categories information and add them to the same array.. any ideas?
Still, even in that situation I would be looking at pulling the data in to the better structure (a la 5tons') via what ever server-side language is in use.
One-liner would do it…
Anyway, sympathies, Legacy Data is Horrid™
-
I know.. But the problem is that the client has sent the data to us as XML and unfortunately, the lazy so and so's dont wanna change it as that would mean they would have to do some work.
I don't mind. Am charging them by the hour, so the more work I have to do the better... LOL
Tags for this Thread
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
|