A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: [RESOLVED] XML comma delimited entries to Array

  1. #1
    Junior Member
    Join Date
    Dec 2009
    Posts
    7

    resolved [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

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    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.

  3. #3
    Junior Member
    Join Date
    Dec 2009
    Posts
    7

    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?

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Code:
    var categories:Array = [];
    var allCategoriesNodes:XMLList = someXMLVariable.shop.categories;
    var categoriesNode:XML;
    for each (categoriesNode in allCategoriesNodes){
      categories.concat(categoriesNode.toString().split(", "));
    }

  5. #5
    Junior Member
    Join Date
    Dec 2009
    Posts
    7
    Thanks 5TonsOfFlax.. just what I needed fella.

  6. #6
    Junior Member
    Join Date
    Apr 2010
    Posts
    11
    Quote Originally Posted by PhilJ2008 View Post
    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™

  7. #7
    Junior Member
    Join Date
    Sep 2007
    Posts
    20
    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
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center