1

I want to convert my xml file to json...but below code shows null pointer exception..i don't know what went wrong.

public class Xmljson {  
    private URL url = null;
    private InputStream inputStream = null;  

    public void getXMLfromJson() {
        try{
            url = Xmljson.class.getClassLoader().getResource("datafile.xml");
            inputStream = url.openStream();
            String xml = IOUtils.toString(inputStream);
            JSON objJson = new XMLSerializer().read(xml);
            System.out.println("JSON data : " + objJson);
        }catch(Exception e){
            e.printStackTrace();
        }finally{
           try {
                if (inputStream != null) {
                    inputStream.close();
                }
                url = null;
            } catch (IOException ex) {}
        }
    }   

    public static void main(String[] args) {
        new Xmljson().getXMLfromJson();
    }
}

shows exception here

         url = Xmlto.class.getClassLoader().getResource("data");
            inputStream = url.openStream();

NPE IS

java.lang.NullPointerException
at pkg.news.Xmlto.getXMLfromJson(Xmlto.java:19)
at pkg.news.Xmlto.main(Xmlto.java:35)

referred from

http://tutorial4java.blogspot.in/2013/04/xml-to-json-conversion.html

1
  • 2
    Indicate the NPE line, but I'm assuming the resource can not be found. Commented Sep 17, 2014 at 6:01

2 Answers 2

5

Check this out...this works perfect....

  public class Xml2json {

    static String line="",str="";
    public static void main(String[] args) throws JSONException, IOException {
        String link = "data.xml";
        BufferedReader br = new BufferedReader(new FileReader(link));
        while ((line = br.readLine()) != null) 
        {   
            str+=line;  
        }
        JSONObject jsondata = XML.toJSONObject(str);
        System.out.println(jsondata);
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

What is XML? /////
@EvgeniiVorobei XML (eXtensible Markup Language) is another format of keeping the key-value in the form of HTML concept. For example of XML <root> <name>JSON</name> <integer>1</integer> <double>2.0</double> </root> and in json format { "name": "JSON", "integer": "1", "double": "2.0" }
@AzrielTan, I mean what class is it? What package? What gradle dependency?
0

It should work

url = XMLjson.class.getClassLoader().getResource("datafile.xml");

Have you added these statements as well,

import java.io.InputStream;  
import java.net.URL;  
import net.sf.json.JSON;  
import net.sf.json.xml.XMLSerializer;  
import org.apache.commons.io.IOUtils;  

Take a look at here as well http://tutorial4java.blogspot.in/2013/04/xml-to-json-conversion.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.