5

Basically i want to create XMLDesigner kind of thing in Flex, using which user can add/edit components and properties of view/dashboard. i am storing view structure in a xml file. i parsed that file at runtime and display view. How to convert an object (having properties and sub-objects) to xml node (having attributes and elements) and add that xml to the existing xml file. so that next time when i parsed xml file i'll get that new component in my view/dashboard.

for e.g, object structure of component in xml file :

<view id="productView" label="Products">
<panel id="chartPanel" type="CHART" ChartType="Pie2D" title="Productwise Sales"  x="215" y="80" width="425" height="240" showValues="0" >  
  </panel> 
</view>

Thanks in Advance.

2 Answers 2

7

Use an XML (de)serialization library.

There are many out there but one thing that I have used and found very stable is FlexxB. It has got a plethora of functions and I swear by it!

Flexxb is annotation based and very easy to use once you get the hang of it.

Here is a sample copied from the main web site.

        [XmlClass(alias="MOck2Replacement", idField="id")]
        public class Mock3
        {
                [XmlAttribute]
                public var id : Number = 3;
                [XmlAttribute]
                public var attribute : Boolean;
                [XmlElement(alias="objectVersion")]
                public var version : Number;

                public function Mock3()
                {
                        super();
                }
        }

You decorate each of your variables with an annotation and tell what kind of XML type would it be.

And you create the object

var target : Mock3 = new Mock3();
target.attribute = true;
target.id = 5;
target.version = 33;

And you do

FlexXBEngine.instance.serialize(target)

And the result would be

<MOck2Replacement attribute="true" id="5">
  <objectVersion>
    33
  </objectVersion>
</MOck2Replacement>

And the other AWESOME thing about is that you don't have to have the source of an object to decorate it.

There is an API that handle that. Using this AP, I successfully de(serialized) SolidColor and LinearGradient where I did not have the source to decorate it.

Checkout the General Description and Samples. And it's a one SWC install :)

Sign up to request clarification or add additional context in comments.

10 Comments

Hi Ranhiru, Thanks for the link it is almost the same as i needed. In this first we have to define classes for each component and define their attributes and elements.Is it? What if we want to add new attributes to components for e.g. < MOck2Replacement attribute="true" id="5" fontSize="14"> In this case we have to first define fontSize attribute in Mock3 class. Can we do this things using flexXB?? how can i add new attributes/elements to the Mock3 class at runtime??
Yes. That is the easy way when you know all the attributes. Or else you can use the Annotation API to register the annotations at run time. Check the Programatically building descriptor section in code.google.com/p/flexxb/wiki/Samples2x
In that example they havn't added any new attribute or elements to the class. Can we do that using Annotation API??
Wait! So this new attribute is NOT there in your object but you want to add it to the XML? Or the new attribute gets added to your object at run time ?
It's better to raise that concern as a separate question. But since Flexxb has the Annotation API you will be able to serialize it. Or else you can try using SimpleXMLEncoder and SimpleXMLDecoder. blog.flexexamples.com/2008/03/04/…
|
0

I would use asbeangen for the job.

  1. You define a dtd
  2. You generate actionscript model classes from the dtd
  3. In your actionscript code you load your xml and fill the generated model with the values
  4. You modify the model and call toXML() on it's root. That gives you a xml with the modyfied values.

Here is a more elaborate quickstart.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.