将Json转换为XML生成的无效XML [英] Converting Json to XML Generated invalid XML

查看:335
本文介绍了将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
{

//连接到S3
s3 = new AmazonS3Client(新的ClasspathPropertiesFileCredentialsProvider());
区域usWest2 = Region.getRegion(Regions.US_EAST_1);
s3.setRegion(usWest2);

//下载对象
System.out.println(下载对象);
S3Object s3Object = s3.getObject(new GetObjectRequest(inputBucket,inputFile));
System.out.println(Content-Type:+ s3Object.getObjectMetadata()。getContentType());


//读取JSON文件
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);

//转换为XML
String xml = XML.toString(jsonArray);

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

}
}

这是Json文件的外观像

  [
{
_type:ArticleItem,
body :谁签名,
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或无任何文本的文件。在最后一个<> 之后,它仍然生成一些文本,所以整个文件无效。这里有什么问题?请帮助。



更新



根据 jtahlborn的答案

< / 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></阵列>

但在这里说:

  XML解析错误:垃圾后文档元素
位置:http://www.w3schools.com/xml/xml_validator.asp
行号1,列181:


解决方案

您需要 flush() / close() / code> FileWriter 以确保将所有数据写入文件。



问题是您的xml结果(2数组元素)中有2个顶级元素。 xml只能有一个顶级元素。



更新:



尝试将json转换为xml :

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


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? Please help.

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:

解决方案

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

这篇关于将Json转换为XML生成的无效XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆