1

I am trying to parse a json file using the following code as

public class JSONParsing {

private static final String XMLOutput = "C:/Users/Content/Downloads/outputxml.txt";

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

        try{


        String URLfile = "xxxx"


        // Step 1> 
        // Connecting to the Website to get the JSON file
        URL url = new URL(URLfile);
        URLConnection conn = url.openConnection();

        // Step 2> to get the information as an 'input stream'
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse(conn.getInputStream());

        TransformerFactory tfactory = TransformerFactory.newInstance();
        Transformer xform = tfactory.newTransformer();

        File myOutput = new File(XMLOutput);
        xform.transform(new DOMSource(doc), new StreamResult(myOutput));
    InputStream is = 
            JSONParsing.class.getResourceAsStream( "C:/Users/Content/Downloads/outputxml.txt");
    String jsonTxt = IOUtils.toString( is );

    JSONObject json = (JSONObject) JSONSerializer.toJSON( jsonTxt );        
    int listingCount = json.getInt( "listingCount" );
    double totalBudget = json.getDouble( "totalBudget" );
    double cpc = json.getDouble("cpc");


    System.out.println( "Listing Count: " + listingCount );
    System.out.println( "Total Budget: " + totalBudget );
    System.out.println( "CPC :"+cpc);

}catch(Exception e){
    System.out.println("There is an error :"+e);
}

}

}

My input file is in this format as -

{"listingCount":10,"totalBudget":25000.00,"cpc":0.54,"ctr":null}

Error

 Error : [Fatal Error] :1:1: Content is not allowed in prolog.
 There is an error :org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog.

Any help is appreciated.

user3188390
  • 603
  • 2
  • 11
  • 19
  • Your input is not a valid xml doc (it's json) –  Apr 05 '14 at 18:36
  • Read [Java parsing XML document gives “Content not allowed in prolog.” error](http://stackoverflow.com/questions/2599919/java-parsing-xml-document-gives-content-not-allowed-in-prolog-error) – Braj Apr 05 '14 at 18:42
  • Why are you trying to parse JSON with an XML parser? – Ian Roberts Apr 05 '14 at 19:17
  • @IanRoberts : Sorry this is the first time I am trying to parse a JSON, Are you asking about the JSON Parser : JSONParsing or you asking about "DOM" builder. Please help me to understand it. – user3188390 Apr 05 '14 at 19:30
  • Your current code makes no sense and it looks like you've just stuck together two unrelated bits of example code for different purposes. The first part of your method downloads a URL, parses it as XML and then immediately re-serializes the resulting DOM tree to an XML file on disk. The second part of the method loads the same XML file from disk, parses it as JSON (?!) and then extracts some properties from it. The error message is from the first part, where it tries to parse `URLFile` as XML and can't. – Ian Roberts Apr 05 '14 at 21:44
  • 2
    If `URLFile` is really JSON then scrap the whole "step 2" section and just say `String jsonText = IOUtils.toString(conn.getInputStream(), "UTF-8");` – Ian Roberts Apr 05 '14 at 21:50

0 Answers0