PeakGen
PeakGen

Reputation: 22995

Converting JSON to XML generated invalid XML

Please have a look at the following.

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONML;
import org.json.JSONTokener;
import org.json.XML;

import com.amazonaws.auth.ClasspathPropertiesFileCredentialsProvider;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3Client;
import com.amazonaws.services.s3.model.GetObjectRequest;
import com.amazonaws.services.s3.model.S3Object;

public class JsonToXML
{
    private AmazonS3Client s3;


    public JsonToXML(String inputBucket, String inputFile) throws IOException, JSONException
    {

        //Connection to S3
        s3 = new AmazonS3Client(new ClasspathPropertiesFileCredentialsProvider());
        Region usWest2 = Region.getRegion(Regions.US_EAST_1);
        s3.setRegion(usWest2);

        //Downloading the Object
        System.out.println("Downloading Object");
        S3Object s3Object = s3.getObject(new GetObjectRequest(inputBucket, inputFile));
        System.out.println("Content-Type: "  + s3Object.getObjectMetadata().getContentType());


        //Read the JSON File
        BufferedReader reader = new BufferedReader(new InputStreamReader(s3Object.getObjectContent()));
        StringBuffer strBuffer = new StringBuffer("");
        int i=0;
        while (true) {
            String line = reader.readLine();
            if (line == null) break;

            System.out.println("Running: "+i);
            strBuffer.append(line);
            i++;
        }

        JSONTokener jTokener = new JSONTokener(strBuffer.toString());
        JSONArray jsonArray = new JSONArray(jTokener);

        //Convert to XML
        String xml = XML.toString(jsonArray);

        File f = new File("XML.xml");
        FileWriter fw = new FileWriter(f);
        fw.write(xml);

    }
}

This is how the Json files look like

     [
    {
        "_type": "ArticleItem",
        "body": "Who's signing",
        "source": "money.cnn.com",
        "last_crawl_date": "2014-01-14",
        "url": "http: //money.cnn.com/"
    },
    {
        "_type": "ArticleItem",
        "body": "GMreveals",
        "title": "GMreveals625-horsepowerCorvetteZ06-Jan.13",
        "source": "money.cnn.com",
        "last_crawl_date": "2014-01-14",
        "url": "http: //money.cnn.com"
    }
]

This code generated invalid XML or files without any text. Invalid means, after the last <> it still generate some text, so the entire file is invalid. What is wrong here?

UPDATE

According to the answer of jtahlborn I managed to generate an XML file with the following output.

   <array><body>Who&apos;s signing</body><_type>ArticleItem</_type><source>money.cnn.com</source><last_crawl_date>2014-01-14</last_crawl_date><url>http: //money.cnn.com/</url></array><array><body>GMreveals</body><_type>ArticleItem</_type><title>GMreveals625-horsepowerCorvetteZ06-Jan.13</title><source>money.cnn.com</source><last_crawl_date>2014-01-14</last_crawl_date><url>http: //money.cnn.com</url></array>

But XML Validator in here says:

XML Parsing Error: junk after document element
Location: http://www.w3schools.com/xml/xml_validator.asp
Line Number 1, Column 181:

Upvotes: 0

Views: 1378

Answers (1)

jtahlborn
jtahlborn

Reputation: 53589

You need to flush()/close() the FileWriter to ensure all the data is written to the file.

The problem is that you have 2 "top-level" elements in your xml result (2 "array" elements). xml can only have one top-level element.

UPDATE:

Try this for converting the json to xml:

String xml = XML.toString(jsonArray, "doc");

Upvotes: 2

Related Questions