Converting Action Script object to XML

The Flex and XML Portlet is sample of how you can send XML String from Java Portlet to Flex and Flex displays it in DataGrid format. In that blog i covered how to submit data entered by user in Flex application, using HTTP POST method.

I changed the Sample code so that now i am converting the data submitted by user in Flex application into XML first then submitting it using HTTP POST method. I am using As3xml library for converting Action Script object into XML.

In order to get this sample working first i had to define Flex Contact class then change the addContact() method like this

public function addContact():void{
trace("Entering useHttpService");
service = new HTTPService();
service.url = nextActionURL;
service.resultFormat = HTTPService.RESULT_FORMAT_TEXT;
service.method = "POST";
var myContact:Contact = getContact();

var myContactXML:XML= Asx3mer.instance.toXML(myContact);

var params:Object = { myContactStr: myContactXML };

trace("Entering useHttpService");
public function getContact():Contact{
var myContact:Contact =new Contact();
myContact.firstName = txtFirstName.text;
myContact.lastName = txtLastName.text; = txtEmail.text;
return myContact;

First i did create a getContact() method which reads values entered by user in Flex application and returns a Contact object based on it. Then converting Contact Flex object into XML object is pretty simple all i had to do was call Asx3mer.instance.toXML(). What i liked most is about working with XML in Flex is how easy it is pretty print XML, all i had to do was print the XML object and it prints a XML like this


I am submitting a form to the actionURL with this XML as value of myContactStr

Then on the server side i had to create a method which can parse the XML string into Java Object, i am using Xstream for that

public static Contact convertXMLToJava(String xmlStr){
XStream xstream = new XStream(new DomDriver());
xstream.alias("com.webspherenotes.flex.Contact", Contact.class);
System.out.println("setting values of contact object");
Contact contact= (Contact)xstream.fromXML(xmlStr);
return contact;


Unknown said...

I have unbox so many company like nokia lenove etc if you want to unbox something ask me

srjwebsolutions said...

We are leading responsive website designing and development company in Noida.
We are offering mobile friendly responsive website designing, website development, e-commerce website, seo service and sem services in Noida.

Responsive Website Designing Company in Noida
Website Designing Company in Noida
SEO Services in Noida
SMO Services in Noida

Vikas Chaudhary said...

Battery Mantra is Authorized exide car battery dealer in Noida and Greater Noida. We are providing our service in Indirapuram, Delhi, Ashok Nagar.

Exide Battery Dealer in Noida
Battery Dealer in Noida
Authorized Battery Dealer in Noida
Car Battery Dealer in Noida
Car Battery Dealer
Exide Battery Dealer

EG MEDI said... is online medical store pharmacy in laxmi nagar Delhi. You can Order prescription/OTC medicines online.
Cash on Delivery available. Free Home Delivery

Online Pharmacy in Delhi
Buy Online medicine in Delhi
Online Pharmacy in laxmi nagar
Buy Online medicine in laxmi nagar
Onine Medical Store in Delhi
Online Medical store in laxmi nagar
Online medicine store in delhi
online medicine store in laxmi nagar
Purchase Medicine Online
Online Pharmacy India
Online Medical Store