PDA

View Full Version : how to get XML object from Flex to Servlet


Himi
11-14-2008, 06:33 PM
Hi
I have an application in which the the client gets sopme XML from the server, modifies it and has to write this changed XML back to a file on the server.
The client side is on flex and server is a servlet. So i made an XML object in the flex code which i am passing to the server but i dont understand how to retrieve it in the servlet code.

the flex code is
private function sendWrite() : void
{
var http:HTTPService = new HTTPService();
http.url = ROOT_URL;
http.resultFormat = "text";
http.method="post";
http.contentType="application/xml";
http.request=xmlObj; // This is the xml object that needs to be passed.
http.addEventListener(ResultEvent.RESULT, onResult);
http.addEventListener(FaultEvent.FAULT, onFault);
http.send();
}





My servlet code is :

public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
// I need help here . How do i retrieve the xml object?
// The getParameters method only returns strings.. Is there a way to get object ?
}


btw,
The xml im sending from flex has the format :

<A type="main" >
<B type="sub" value="1" />
<B type="sub" value="2" />
<B type="sub" value="3" />

</A>



Thanks,
Himi

Sly_cardinal
11-16-2008, 01:01 PM
I don't think that a basic servlet will be able to manage mapping an Actionscript XML object directly to a Java object (not without a fair bit of work at least).

The Flex documentation for the HTTPService class indicates that the 'request' parameter is an object of name/value pairs. I don't think that you are setting the variable correctly in your code example

If you make the following change:


var request:Object = {xml:xmlObj.toString()};
http.request = request;


Are you able to then get the XML string representation from the request?

e.g.

public void doPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
// log out: request.getParameter("xml");
}