Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

xml-josn object or string

 
jyothsna ananthula
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi. please any body suggest me how to convert xml file to json object.
any process please urgent
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As for "urgent", please read this: EaseUp.

Let me point out that there are XML documents which can't be expressed as JSON documents. XML is just more complex than JSON is. But having said that, the way I would approach this would be to use the google keywords "xml convert json" and see what turns up.

Or did you already try that? What did you try so far?
 
jyothsna ananthula
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:As for "urgent", please read this: EaseUp.

Let me point out that there are XML documents which can't be expressed as JSON documents. XML is just more complex than JSON is. But having said that, the way I would approach this would be to use the google keywords "xml convert json" and see what turns up.

Or did you already try that? What did you try so far?



yes i tried in google with xml-json and xml file to convert to json.

i developed the following code, in this i am unable to find the jars what ever needed for this application.and could you please suggest me for example org.json.XML is one package so for if use this package methods in class getting error.so i came to know jar is not available.but what type of jar i need to download.for this i came to know to download json-org jar.please let me know how to know jar needed for particular application.

import java.io.*;


import net.sf.json.JSONObject;
import net.sf.json.xml.XMLSerializer;
import org.apache.commons.io.IOUtils;
import net.sf.json.JSON;






class ConvertToJsonObject {

public static void main(String[] args) throws IOException{

InputStream file1 = new FileInputStream(new File("C:\\Documents and Settings\\JA0084604\\Desktop\\NMS\\XO-OpenNMS\\build.xml"));


try {
JSONObject xmlJSONObj=new JSONObject();

xmlJSONObj = XML.toJSONObject(file1);
String jsonPrettyPrintString = xmlJSONObj.toString(PRETTY_PRINT_INDENT_FACTOR);

System.out.println(jsonPrettyPrintString);
} catch (JSONException je) {
System.out.println(je.toString());
}
}}

/* String xml = IOUtils.toString(file1);

JSONObject xmlJSONObj=new JSONObject();

XMLSerializer xmlSerializer = new XMLSerializer();


JSON json= xmlSerializer.read(xml);
System.out.println( json.toString(2) );


}*/

paul please help me in the following program

import java.io.*;


import net.sf.json.JSONObject;
import net.sf.json.xml.XMLSerializer;
import org.apache.commons.io.IOUtils;

import org.json.XML;






class ConvertToJsonObject {

public static void main(String[] args) throws IOException{

InputStream file1 = new FileInputStream(new File("C:\\Documents and Settings\\JA0084604\\Desktop\\NMS\\XO-OpenNMS\\build.xml"));

String xml = IOUtils.toString(file1);
try {
JSONObject xmlJSONObj=new JSONObject();

xmlJSONObj = XML.toJSONObject(xml); // in this line error getting as required net.sf.json.JSONObject found org.json.josn object
String jsonPrettyPrintString = xmlJSONObj.toString(2);

System.out.println(jsonPrettyPrintString);
} catch (JSONException je) {
System.out.println(je.toString());
}
}}


please help me how to resolve


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic