Conversion from JSON to XML and XML to JSON

179 views
Skip to first unread message

Malli

unread,
Jun 14, 2010, 2:14:48 AM6/14/10
to Google Web Toolkit
Hi ,

I'm using GWT2.0 I need converter from JSON to XML and XML to JSON..is
there any pre-defined function for converting from JSON to XML and
XML to JSON.If its so .....please tel me what that good function ..
If we don't have any function please tel me how ..we can do ?

Thanks,
Malli.

Jan Ehrhardt

unread,
Jun 14, 2010, 4:14:21 AM6/14/10
to google-we...@googlegroups.com
There is no such method that does job.

It's not really clear to me, why you will do this conversion or better where? Do you want to convert it in the client (as part of your GWT app)? If so, what are you doing with the two formats? Maybe it is not really required, so could you give me some more details on this?

Regards
Jan Ehrhardt

2010/6/14 Malli <mallisev...@gmail.com>

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-we...@googlegroups.com.
To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.


malliseven.hills

unread,
Jun 14, 2010, 6:28:49 AM6/14/10
to google-we...@googlegroups.com
Hi Jan,
Thanks for quick reply..

Yes .....your  correct on conversion ...

Here i'm explaining my problem  please spend your valuable time for get out me from  my problems.

1.) I'm using JSON Request and JSON  Response with RestFull.

Here my Restfull configuration  is ..

 <util:map id="nstojns" map-class="java.util.HashMap">
    <entry key="http://cxf.apache.org/bindings/xformat" value="cxf"/>
    <entry key="http://server.sample.com/" value=""/>
    <entry key="http://www.w3.org/2001/XMLSchema-instance" value="w3c"/>   
  </util:map>

 <bean id="xmlInputFactory" class="org.codehaus.jettison.mapped.MappedXMLInputFactory">
    <constructor-arg>
      <ref bean="nstojns" />
    </constructor-arg>
  </bean>
 
  <bean id="xmlOutputFactory" class="com.sample.codehaus.jettison.mapped.MappedXMLOutputFactory">
    <constructor-arg>
      <ref bean="nstojns"/>
    </constructor-arg>
  </bean>

[I have overridden MappedXMLOutputFactory.java for rectifying the some array issues i.e
 you can see the following link *http://jira.codehaus.org/browse/JETTISON-44 *  what and all issues with Jettison MappedXMLOutputFactory.java 

NOTE: before overridden and after
overridden i'm getting same problems.  ]


  <jaxws:endpoint id="SampleRESTFullService" implementor="#SampleRESTFullService"  address="/sample/json"  bindingUri="http://apache.org/cxf/binding/http">
    <jaxws:serviceFactory>
      <bean class="org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean">
        <property name="properties">
          <map>
            <entry key="javax.xml.stream.XMLInputFactory" value-ref="xmlInputFactory" />
            <entry key="javax.xml.stream.XMLOutputFactory" value-ref="xmlOutputFactory"/>
            <entry key="Content-Type" value="application/json
"/>
          </map>
        </property>
        <property name="wrapped" value="true"/>
        <property name="qualifyWrapperSchema" value="true" />
      </bean>
    </jaxws:serviceFactory>
  </jaxws:endpoint>


upto here all is fine i.e mapping JSON Object to POJO ..and perform my business logic and send repose as JSON  Format to UI .

2.) but my problem started here  when send response to UI ..its set the ****** response content type to text/xml********  but response
{"getRolesResponse":{"return":["Executive","Manager","SuperAdmin"]}} so the response format JSON Response format.. thatsy browser showing error ..***not well-formed**

So if i want (should be) rectify this problems ..i have to change the *******response content type to application/json****

3.) is there any way to change the response content type to application/json?.
please help me out if have any way,and tel me what i have to do.(I tried through filter but its not helped me out).

Thanks,
Malli.

Jan Ehrhardt

unread,
Jun 14, 2010, 8:30:59 AM6/14/10
to google-we...@googlegroups.com
Ok, you mean JSONPRequest and JSONPResponse, or?

JSON-P is a little different from normal JSON, it requires to be wrapped in a callback function. Your response should look like this:

callback( {"getRolesResponse":{"return":["Executive","Manager","SuperAdmin"]}} );

The call to the callback function is required to make cross domain Ajax work. GWT uses a function named 'callback' by default, but you can change it.

You'll need to extend your server code to create the above result:

jsonpResponse = "callback(" + generatedJsonResponse + ");"

where "generatedJsonResponse" is the JSON generated by your JSON library.

You should only use JSON-P, if you want do do cross-domain Ajax. If cross-domain is not required, use the RequestBuilder and JSONParser to do a normal Ajax request and parse the resulting String.

Regards
Jan Ehrhardt

2010/6/14 malliseven.hills <mallisev...@gmail.com>

malliseven.hills

unread,
Jun 14, 2010, 10:33:46 AM6/14/10
to google-we...@googlegroups.com
Hi Jan,
thanks for your reply..

I'm not using JSONPRequest and JSONPResponse  .
I'm using normal JSON Request and  normal JSON  Response by using RequestBuilder..

Here i'm pasting how i'm preparing JSON Request ..


      com.google.gwt.json.client.JSONObject lInnerObject = new com.google.gwt.json.client.JSONObject();
        lInnerObject.put("status", new com.google.gwt.json.client.JSONString ("Active"));
        lInnerObject.put("userName", new  com.google.gwt.json.client.JSONString ("test"));

        com.google.gwt.json.client.JSONObject lWrapperObject =new com.google.gwt.json.client.JSONObject();
        lWrapperObject.put("StateRequest", lInnerObject);

Then i'm using RequestBuilder for calling my restfull services..
after got the response i'm using JSONParser  to parsing my reponse .
 JSONObject lObject = (JSONObject) JSONParser.parse(lResponse.getText());

all the functions are working fine ..Problem is if we use FF ..its having option ***********Error Console****** under  Tools..If  I click on this option its showing
***not well-formed** ..because CXF  set the response content type to *text/xml*  and its  sending JSON format..but Browser expecting xml format since  response
content type is **text/xml** ..so if we change the response content type to **application/json ** ..then i think problem would be solved.

Thanks,
Malli


Jan Ehrhardt

unread,
Jun 14, 2010, 10:44:35 AM6/14/10
to google-we...@googlegroups.com
Ah, ok.

First do you set the Accept header in your RequestBuilder? It tells the server which type is accepted.

myRequestBuilder.setHeader("Accept", "application/json");

This will tell the server to serve JSON. If your server handles it correctly, it will work. If it doesn't help, it seems, that your server configuration is not correct.

Probably you can also try content type text/json.

malliseven.hills

unread,
Jun 15, 2010, 1:44:15 AM6/15/10
to google-we...@googlegroups.com
Hi Jan,,

Still i'm getting same problem even i set the  myRequestBuilder.setHeader("Accept", "application/json");
Probably you can also try content type text/json.

and i'm pasting my server config here  ..Please tel me where could  be the problem.
Please give me some examples which are working fine with JSON Request and JSON Response  with combination of Restfull.
Reply all
Reply to author
Forward
0 new messages