Tutorial : Flex convert object to xml
by toy
This is CustomerDTO.as that extends DTOBean.as
This is the base class for every DTO.
1
2
3
4
5
6
7
8
9
10 package actionScript
{
public class DTOBean
{
public function DTOBean()
{
}
}
}
Here is our hero ObjectConverter.as
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 package actionScript
{
import flash.xml.XMLDocument;
import flash.xml.XMLNode;
import mx.rpc.xml.SimpleXMLEncoder;
public class ObjectConverter
{
public function ObjectConverter()
{
}
/**
* Converting DTO Bean object to XML
*/
public function objectToXML(dtoBean:DTOBean):XML
{
var qName:QName = new QName("form");
var xmlDocument:XMLDocument = new XMLDocument();
var simpleXMLEncoder:SimpleXMLEncoder = new SimpleXMLEncoder(xmlDocument);
var xmlNode:XMLNode = simpleXMLEncoder.encodeValue(dtoBean, qName, xmlDocument);
var xml:XML = new XML(xmlDocument.toString());
return xml;
}
}
}
The last one
1
2
3
4
5
6
7
8
9
10
11
12
13 import mx.controls.Alert;
import actionScript.ObjectConverter;
import actionScript.CustomerDTO;
var converter:ObjectConverter = new ObjectConverter();
var customer:CustomerDTO = new CustomerDTO();
customer.name = "Noppanit";
customer.surname = "Charassinvichai";
var xml:XML = converter.objectToXML( customer );
Alert.show( xml.toXMLString() );

Twitter
Facebook