Solved

How do we convert flat arraycollection into XMLList ?

Posted on 2010-09-06
16
850 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
Comment Utility
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
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
Do you have PHP available?
0
 
LVL 1

Author Comment

by:myBlueSky
Comment Utility
Ray_Paseur,

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

Expert Comment

by:heppa
Comment Utility
you might want to look at SimpleXMLEncoder#encodeValue().

Good luck!
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 4

Expert Comment

by:heppa
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thank you for your participation.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The goal of the tutorial is to teach the user how to how to record live broadcast.

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now