Here is how I convert XML from a SOAP web service into a VO.
The VO Class:
package vo
{
[Bindable]
public class Product
{
public var ID:String;
public var Category:String;
public var Price:String;
public var Name:String;
public function
Product(_ID:String,_Category:String,_Price:String,_Name:String)
{
this.ID=_ID;
this.Category=_Category;
this.Price=_Price;
this.Name=_Name;
}
}
}
The web service result handler and namespace stripper:
private function wsProductsResult(event:ResultEvent):void
{
var xmlResult:XMLList = event.result as XMLList;
var xmlSource:String = xmlResult.toString();
//Strip namespace
xmlSource = xmlSource.replace(/<[^!?]?[^>]+?>/g, removeNamspaces);
xmlResult = XMLList(xmlSource);
//wrap XMLList in XMLListCollection
var productsXmlc:XMLListCollection = new
XMLListCollection(xmlResult.children());
var productsAC:ArrayCollection = new ArrayCollection();
//Cast xml element items into array of value objects
for(var i:int=0;i<productsXmlc.length;i++)
{
var productTemp:Product = new Product(
productsXmlc.getItemAt(i)..ID,
productsXmlc.getItemAt(i)..Category,
productsXmlc.getItemAt(i)..Price,
productsXmlc.getItemAt(i)..Name);
productsAC.addItem(productTemp);
}
//Bind ArrayCollection to Custom Component
dgProducts.dg.dataProvider=productsAC;
}
public function removeNamspaces(...rest):String
{
rest[0] = rest[0].replace(/xmlns[^"]+\"[^"]+\"/g, "");
var attrs:Array = rest[0].match(/\"[^"]*\"/g);
rest[0] = rest[0].replace(/\"[^"]*\"/g, "%attribute value%");
rest[0] = rest[0].replace(/(<\/?|\s)\w+\:/g, "$1");
while (rest[0].indexOf("%attribute value%") > 0)
{
rest[0] = rest[0].replace("%attribute value%", attrs.shift());
}
return rest[0];
}
--- In [email protected], "W.R. de Boer" <w...@...> wrote:
>
> Hello,
>
> I am having a question what the best approach is to convert existing XML data
> to a object graph of value objects. I have spend quite some time to fix a
> memory leak in my existing parsing practice and I am curious how others solve
> this problem.
>
> My common approach is to create a class such as EventReader and
> EventItemReader class which is responsible for the parsing of the specific
> XML element and return the appropriate value object. But somehow this code is
> leaking like a mad dog (200kb per time) while the XML file is only 16kb. Now
> I have currently rewritten it so that just using simple strong typed objects.
>
> But I would love to find out what I am doing wrong in my current approach and
> how to solve it. Because it makes it easier to reuse the parsing code/logic.
>
> For example, normally, I use the approach of creating a public class with a
> few public variables like this:
>
> public class EventItemVO {
> public var eventDate: Date;
> public var eventName: String;
> public var location: String;
> }
>
> and then I am having code like this to parse it:
>
> try {
> var xml: XML = new XML( loaderContent );
> var parser: EventItemXMLReader = new EventItemXMLReader( xml );
> parser.parse();
> } catch (e: Error) {
> trace("Error occured while parsing");
> } finally {
> parser = null;
> xml = null;
> }
>
> in the EventItemXMLReader-class I then return an instance of the
> EventItemVO-class
>
> Somehow this leaks and while I do the same stuff in the onComplete-handler of
> the Loader and do:
> array.push( {eventDate:date, eventName:name, location:location} );
>
> The memory leak disappears. I would expect their is some reference kept alive
> but I am having a hard-time finding the reference which keeps it from garbage
> collecting it all.
>
> How do you parse XML and convert it in value objects?
>
> Yours,
> Weyert de Boer
>