Solved

How do we convert flat arraycollection into XMLList ?

Posted on 2010-09-06
16
856 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 110

Expert Comment

by:Ray Paseur
ID: 33645427
Do you have PHP available?
0
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 
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

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

Question has a verified solution.

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

Learn by example how to specify CSS selectors for Selenium WebDriver test automation software.
Get to know the ins and outs of building a web-based ERP system for your enterprise. Development timeline, technology, and costs outlined.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

728 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