Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to Check Whether it is XML or JSON response in JAVA?  RSS feed

 
Durga Roobini
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am going to access third party webservices API let we assume job category ,i use URL to get response from corresponding third party webservices,it returns response whether it is xml or json,
1.how to check incoming response in XML or JSON?
2.If it is JSON means how to convert it to XML?

PLease give me the solution with EG
 
T.A. Nguyen
Ranch Hand
Posts: 36
Eclipse IDE Java ME Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should be able to quickly check to see if the result is xml. By XML the data must start with "<" and end ">". For JSON it a little bit more involve but you can learn more here: http://www.json.org/
For converting JSON to XML, the JSON library have an XMLSerializer class that can convert a json object / array into XML.

 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you run JSON through an XML parser, or XML through a JSON parser, you would get exceptions pretty quickly.

As to #2, check out this recent topic: http://www.coderanch.com/t/628876/java-io/java/Converting-Json-XML-Generated-invalid
 
Ron McLeod
Saloon Keeper
Posts: 1425
206
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The web service response should include a HTTP Content-Type header to indicate the type of payload it is returning. For example:

HTTP/1.1 200 Done
Content-Type application/xml
Content-Length 74

<?xml version="1.0" encoding="UTF-8"?>
<confirmation id="35545-AADS-3"/>


Also, you can often influence what you get back by including a HTTP Accept header in your request.
 
Durga Roobini
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public static void main(String[] args) throws Exception {
String jsonData = "{id:123,name:Who}";
XMLSerializer serializer = new XMLSerializer();


Thanks for reply,and sorry for going to ask silly question,in my case
my xml response come from url in this case how i convert to json to xml,what i have to do in String jsonData = "{id:123,name:Who}";


 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how i convert to json to xml,what i have to do in String jsonData = "{id:123,name:Who}";

Please read the topic I linked to. It contains code and important advice on this matter.
 
Durga Roobini
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
T.A. Nguyen wrote:You should be able to quickly check to see if the result is xml. By XML the data must start with "<" and end ">". For JSON it a little bit more involve but you can learn more here: http://www.json.org/
For converting JSON to XML, the JSON library have an XMLSerializer class that can convert a json object / array into XML.




I tried your following code exactly but i cant get the result i add apporiate jars on my lib folder including that common-log 2.2.jar but i get the same error as below

HTTP Status 500 -

--------------------------------------------------------------------------------

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

javax.servlet.ServletException: Servlet execution threw an exception


root cause

java.lang.NoClassDefFoundError: org/apache/commons/lang/exception/NestableRuntimeException
java.lang.ClassLoader.defineClass1(Native Method)
java.lang.ClassLoader.defineClass(Unknown Source)
java.security.SecureClassLoader.defineClass(Unknown Source)
org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:1819)
org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:872)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1327)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1206)
java.lang.ClassLoader.loadClassInternal(Unknown Source)
com.roobi.pulpy.XmlgenPulpy.doPost(XmlgenPulpy.java:120)
javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)


root cause

java.lang.ClassNotFoundException: org.apache.commons.lang.exception.NestableRuntimeException
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1360)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1206)
java.lang.ClassLoader.loadClassInternal(Unknown Source)
java.lang.ClassLoader.defineClass1(Native Method)
java.lang.ClassLoader.defineClass(Unknown Source)
java.security.SecureClassLoader.defineClass(Unknown Source)
org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:1819)
org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:872)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1327)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1206)
java.lang.ClassLoader.loadClassInternal(Unknown Source)
com.roobi.pulpy.XmlgenPulpy.doPost(XmlgenPulpy.java:120)
javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)


note The full stack trace of the root cause is available in the Apache Tomcat/6.0.16 logs.


--------------------------------------------------------------------------------

Apache Tomcat/6.0.16



PLease tell me the solution
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
common-log 2.2.jar

There's no such library, so I'm guessing this is a typo. Anyway, from the code it seems that you need to have Commons IO and Commons Lang (and possibly others they depend on).
 
Ashwani Singh
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:
common-log 2.2.jar

There's no such library, so I'm guessing this is a typo. Anyway, from the code it seems that you need to have Commons IO and Commons Lang (and possibly others they depend on).


There are libraries named:

commons-beanutils-1.9.1
commons-codec-1.6
commons-collections-3.2.1
commons-io-2.4
commons-lang-2.5
commons-logging-1.1.3

Probably inclusion of these libraries might resolve the issue.

Thanks.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!