validating conversion of JSON to XML

扶醉桌前 提交于 2020-03-05 06:06:42

问题


While the JSON is correctly formatted (the jsonTweet variable):

The JSON input is valid according to RFC 4627 (JSON specfication). Close

The JSON input is valid in JavaScript.

However, the xml output is obviously invalid (as xmlStringTweet isn't xml at all).

The key lines, as it were:

        JSONObject jsonTweet = tweets.getJSONObject(Long.toString(id));
        xmlStringTweet = XML.toString(jsonTweet);

where the jsonTweet is found by the "snowflake" id, and, then, ostensibly, converted to an xml string.

full code:

package basex;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Iterator;
import java.util.Properties;
import java.util.logging.Logger;
import org.basex.core.BaseXException;
import org.basex.core.Context;
import org.basex.core.cmd.Add;
import org.basex.core.cmd.Open;
import org.basex.core.cmd.CreateDB;
import org.basex.core.cmd.DropDB;
import org.basex.core.cmd.List;
import org.basex.core.cmd.Set;
import org.json.XML;
import twitter4j.JSONException;
import twitter4j.JSONObject;

public class DatabaseHelper {

    private static final Logger log = Logger.getLogger(DatabaseHelper.class.getName());
    private Properties properties = new Properties();
    private URL url = null;
    private String databaseName = null;
    private Context context = null;
    private String parserType = null;

    private DatabaseHelper() {
    }

    public DatabaseHelper(Properties properties) {
        this.properties = properties;
    }

    private void init() throws MalformedURLException, BaseXException {
        log.fine(properties.toString());
        parserType = properties.getProperty("parserType");
        url = new URL(properties.getProperty(parserType + "URL"));
        databaseName = properties.getProperty("databaseName");
        context = new Context();
        list();
    }

    private void drop() throws BaseXException {
        new Set("parser", parserType).execute(context);
        new DropDB(databaseName).execute(context);
        list();
    }

    private void add(JSONObject tweets) throws JSONException, BaseXException {
        long id = 0L;
        Iterator keys = tweets.keys();
        String xmlStringTweet = null;
        new Open(databaseName).execute(context);
        while (keys.hasNext()) {
            id = Long.parseLong(keys.next().toString());
            JSONObject jsonTweet = tweets.getJSONObject(Long.toString(id));
            xmlStringTweet = XML.toString(jsonTweet);
            log.info(Long.toString(id));
            log.info(jsonTweet.toString());
            log.info(xmlStringTweet);
            new Add(null, xmlStringTweet).execute(context);
        }
    }

    private void create() throws BaseXException, JSONException {
        new Set("parser", parserType).execute(context);
        new CreateDB(databaseName).execute(context);
        new List().execute(context);
        list();
    }

    private void list() throws BaseXException {
        log.fine(new List().execute(context));
    }

    public void dropCreateAdd(JSONObject tweets) throws MalformedURLException, BaseXException, JSONException {
        init();
        drop();
        create();
        add(tweets);
        list();
    }

}

How would I validate that the String of xml is well-formed?

来源:https://stackoverflow.com/questions/60034291/validating-conversion-of-json-to-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!