Solved

How do we convert flat arraycollection into XMLList ?

Posted on 2010-09-06
16
853 Views
Last Modified: 2013-11-19
Hi,
I have the following ArrayCollection.. (How do we convert flat arraycollection into XMLList?)

            [Bindable]
            private var myData:ArrayCollection = new ArrayCollection([
              {Product:"Product-1", id:"100",label:"Item-11", quantity:"10", price:"50", state:"0"},
              {Product:"Product-1", id:"101",label:"Item-12", quantity:"20", price:"10", state:"0"},
              {Product:"Product-1", id:"102",label:"Item-13", quantity:"20", price:"60", state:"0"},  
 
              {Product:"Product-2", id:"200",label:"Item-21", quantity:"10", price:"30", state:"0"},  
              {Product:"Product-2", id:"201",label:"Item-22", quantity:"10", price:"40", state:"0"},  
              {Product:"Product-2", id:"202",label:"Item-23", quantity:"10", price:"90", state:"0"},
             
              {Product:"Product-3", id:"301",label:"Item-31", quantity:"10", price:"80", state:"0"},
              {Product:"Product-3", id:"302",label:"Item-32", quantity:"10", price:"50", state:"0"},
              {Product:"Product-3", id:"303",label:"Item-33", quantity:"10", price:"75", state:"0"},
              {Product:"Product-3", id:"304",label:"Item-33", quantity:"10", price:"45", state:"0"},

              {Product:"Product-4", id:"",label:"", quantity:"", price:""}

            ]);

How do we convert flat arraycollection into XMLList?


it should be like


[Bindable]	private var myXMLList:XMLList =
&#9;&#9;<>
&#9;      <Product id="Product-1"   state="0">
&#9;&#9;      <Item id="100" state="0"  label="Item-11" quantity="10" price="50"/>
&#9;&#9;      <Item id="101" state="0"  label="Item-12" quantity="10" price="10"/>
&#9;&#9;      <Item id="103" state="0"  label="Item-13" quantity="10" price="60"/>
&#9;      </Product>
&#9;      
&#9;      <Product id="Product-2"   state="0">
&#9;&#9;      <Item id="200" state="0"  label="Item-21" quantity="10" price="30"/>
&#9;&#9;      <Item id="201" state="0"  label="Item-22" quantity="10" price="40"/>
&#9;&#9;      <Item id="202" state="0"  label="Item-22" quantity="10" price="90"/>
&#9;      </Product>

&#9;      <Product id="Product-3"   state="0">
&#9;&#9;      <Item id="300" state="0"  label="Item-31" quantity="10" price="80"/>
&#9;&#9;      <Item id="301" state="0"  label="Item-32" quantity="10" price="50"/>
&#9;&#9;      <Item id="302" state="0"  label="Item-33" quantity="10" price="75"/>
&#9;&#9;      <Item id="304" state="0"  label="Item-34" quantity="10" price="45"/>
&#9;      </Product>&#9;

&#9;      <Product id="Product-4"   state="0">
&#9;      </Product>&#9;
&#9;      </>;


Thanks,
0
Comment
Question by:myBlueSky
16 Comments
 
LVL 13

Expert Comment

by:p_nuts
ID: 33616545
hi.

i think this link should show you how to get this done.

http://www.devx.com/getHelpOn/10MinuteSolution/20356

0
 
LVL 1

Author Comment

by:myBlueSky
ID: 33616694
Hi p_nuts,

Thanks for the link. I didn't get that value from the link.

 Do you know how to convert arraycollection into XMLList using actionscript.

0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 33645427
Do you have PHP available?
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 1

Author Comment

by:myBlueSky
ID: 33656451
Ray_Paseur,

No, I don't have PHP available.
0
 
LVL 4

Expert Comment

by:heppa
ID: 33732994
you might want to look at SimpleXMLEncoder#encodeValue().

Good luck!
0
 
LVL 4

Expert Comment

by:heppa
ID: 33733314
I tested a bit around and came up with this:

 private function arrayCollectionToXML(ac:ArrayCollection):XML{
                var xml:XML = objectToXML(ac);
                return xml;
}
                  
                  private function objectToXML(obj:Object):XML {
                 var qName:QName = new QName("root");
                  var xmlDocument:XMLDocument = new XMLDocument();
                  var simpleXMLEncoder:SimpleXMLEncoder = new SimpleXMLEncoder(xmlDocument);
                  var xmlNode:XMLNode = simpleXMLEncoder.encodeValue(obj, qName, xmlDocument);
                  var xml:XML = new XML(xmlDocument.toString());
                  return xml;
                 }

private function init():void{
                       var xml:XML = arrayCollectionToXML(myData);
// now go ahead with your xml that contains <root><list><source><item><id></id>...</item><item>...</source></list></root>
                 }

Good Luck!
0
 
LVL 1

Author Comment

by:myBlueSky
ID: 33758800
what do you mean by this
>>>>> 
// now go ahead with your xml that contains <root><list><source><item><id></id>...</item><item>...</source></list></root>

Can put the complete solution. please have a look on the input data and output result

Thanks,
0
 
LVL 1

Author Comment

by:myBlueSky
ID: 33758808
INPUT :
            [Bindable]
            private var myData:ArrayCollection = new ArrayCollection([
              {Product:"Product-1", id:"100",label:"Item-11", quantity:"10", price:"50", state:"0"},
              {Product:"Product-1", id:"101",label:"Item-12", quantity:"20", price:"10", state:"0"},
              {Product:"Product-1", id:"102",label:"Item-13", quantity:"20", price:"60", state:"0"},  
 
              {Product:"Product-2", id:"200",label:"Item-21", quantity:"10", price:"30", state:"0"},  
              {Product:"Product-2", id:"201",label:"Item-22", quantity:"10", price:"40", state:"0"},  
              {Product:"Product-2", id:"202",label:"Item-23", quantity:"10", price:"90", state:"0"},
             
              {Product:"Product-3", id:"301",label:"Item-31", quantity:"10", price:"80", state:"0"},
              {Product:"Product-3", id:"302",label:"Item-32", quantity:"10", price:"50", state:"0"},
              {Product:"Product-3", id:"303",label:"Item-33", quantity:"10", price:"75", state:"0"},
              {Product:"Product-3", id:"304",label:"Item-33", quantity:"10", price:"45", state:"0"},

              {Product:"Product-4", id:"",label:"", quantity:"", price:""}

            ]);

OUTPUT:



[Bindable] private var myXMLList:XMLList =
<>
      <Product id="Product-1"   state="0">
      <Item id="100" state="0"  label="Item-11" quantity="10" price="50"/>
      <Item id="101" state="0"  label="Item-12" quantity="10" price="10"/>
      <Item id="103" state="0"  label="Item-13" quantity="10" price="60"/>
     </Product>
     
      <Product id="Product-2"   state="0">
      <Item id="200" state="0"  label="Item-21" quantity="10" price="30"/>
      <Item id="201" state="0"  label="Item-22" quantity="10" price="40"/>
      <Item id="202" state="0"  label="Item-22" quantity="10" price="90"/>
      </Product>

     <Product id="Product-3"   state="0">
      <Item id="300" state="0"  label="Item-31" quantity="10" price="80"/>
      <Item id="301" state="0"  label="Item-32" quantity="10" price="50"/>
      <Item id="302" state="0"  label="Item-33" quantity="10" price="75"/>
      <Item id="304" state="0"  label="Item-34" quantity="10" price="45"/>
      </Product>

      <Product id="Product-4"   state="0">
      </Product>&#9;
      </>;

0
 
LVL 4

Accepted Solution

by:
heppa earned 500 total points
ID: 33769124
sorry, couldn't get back to you earlier... and sorry for not recognizing your desired output data structure before. But you should be aware, that both are different in terms of normalization? Your ArrayCollection holds "item" objects whereas your XMList holds "Product" objects. It's still feasible with a bit of processing.

http://www.packtpub.com/article/working-with-xml-in-flex3-and-java-1

This link will help you understand XML processing in flex. you could for example use a function

private function processXMLToXMLList(xml:XML):XMLList{

    // walk through xml to find all items with the same product
    var newXML:XML;
    // now put them all together.
    var list:XMLList  = new XMLList(newXML.list.source.item);
}

now, if you really want the data structure like described above, you could walk through the list, and build a new list out of all items with the same Product-Id. If it's not enough for you to give directions, please let me know.
0
 
LVL 4

Expert Comment

by:heppa
ID: 33980763
Are we supposed to give directions or solve the problem with perfect code? I think, I stated clearly, how you could get a working XML out of an ArrayCollection. Obviously, the OP does not want to dig into the programming language at all. Thanks a lot for making me waste another 30minutes of my life!
0
 
LVL 1

Author Comment

by:myBlueSky
ID: 33980845
Thank you for your participation.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
An enjoyable and seamless user experience can go a long way on an eCommerce site. While a cohesive layout and engaging copy play roles in creating a positive user experience, some sites neglect aspects that seem marginal but in actuality prove very …
The goal of the tutorial is to teach the user how to how to load their YouTube profile onto Flash Media Live Encoder.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question