2014-02-17 113 views
0

請看下面的內容。將JSON轉換爲XML生成的無效XML

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); 

    } 
} 

這是JSON檔案看怎麼樣

 [ 
    { 
     "_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" 
    } 
] 

此代碼生成無效的XML或文件沒有任何文字。無效的手段,在最後的<>之後仍然會生成一些文本,所以整個文件無效。這裏有什麼問題?

UPDATE

根據jtahlborn的答案,我設法生成以下輸出的XML文件。

<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> 

但XML驗證在here說:

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

怎樣的JSON結構看起來像你想轉換成XML?你總是得到同樣的錯誤? –

+0

@PärSvanström:請參閱編輯。是的,錯誤總是生成。 –

+0

顯示生成的xml – jtahlborn

回答

2

您需要flush()/close()FileWriter,以確保所有的數據被寫入到文件中。

問題是,您的XML結果中有2個「頂級」元素(2個「數組」元素)。 xml只能有一個頂級元素。

UPDATE:

試試這個爲JSON轉換成XML:

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

嗨,非常感謝您的回覆。我做了一個更新。請看一看。 –

+0

你好,我只有一個數組。它是一個大Json數組。那麼這意味着這個Json不能被轉換? –

+0

@GloryOfSuccess - 它看起來像你的json和xml不匹配。你的json顯示一個包含多個對象的單個數組,但是你的xml似乎將每個對象分解成一個單獨的數組。你確定你發佈的代碼,json和xml是正確的嗎? – jtahlborn