?
Solved

How do we convert flat arraycollection into XMLList ?

Posted on 2010-09-06
16
Medium Priority
?
857 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 111

Expert Comment

by:Ray Paseur
ID: 33645427
Do you have PHP available?
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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 2000 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

Get proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!

Question has a verified solution.

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

Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
Australian government abolished Visa 457 earlier this April and this article describes how this decision might affect Australian IT scene and IT experts.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

771 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