how to convert arbitrary JSON to XML using BaseX?

*爱你&永不变心* 提交于 2020-03-21 06:42:15

问题


How is arbitrary JSON converted to arbitrary XML using BaseX?

I'm looking at JsonParser from BaseX for this specific solution.

In this case, I have tweets using Twitter4J:

package twitterBaseX;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.logging.Logger;
import main.LoadProps;
import org.basex.core.BaseXException;
import twitter4j.JSONException;
import twitter4j.JSONObject;
import twitter4j.Query;
import twitter4j.QueryResult;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.TwitterObjectFactory;
import twitter4j.conf.ConfigurationBuilder;

public class TwitterOps {

    private static final Logger log = Logger.getLogger(TwitterOps.class.getName());

    public TwitterOps() {
    }

    private TwitterFactory configTwitterFactory() throws IOException {
        LoadProps loadTwitterProps = new LoadProps("twitter");
        Properties properties = loadTwitterProps.loadProperties();
        log.fine(properties.toString());
        ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();

        configurationBuilder.setDebugEnabled(true)
                .setJSONStoreEnabled(true)
                .setOAuthConsumerKey(properties.getProperty("oAuthConsumerKey"))
                .setOAuthConsumerSecret(properties.getProperty("oAuthConsumerSecret"))
                .setOAuthAccessToken(properties.getProperty("oAuthAccessToken"))
                .setOAuthAccessTokenSecret(properties.getProperty("oAuthAccessTokenSecret"));

        return new TwitterFactory(configurationBuilder.build());
    }

    public List<JSONObject> getTweets() throws TwitterException, IOException, JSONException {
        Twitter twitter = configTwitterFactory().getInstance();

        Query query = new Query("lizardbill");
        QueryResult result = twitter.search(query);
        String string = null;
        JSONObject tweet = null;
        List<JSONObject> tweets = new ArrayList<>();

        for (Status status : result.getTweets()) {
            tweet = jsonOps(status);
            tweets.add(tweet);
        }
        return tweets;
    }

    private JSONObject jsonOps(Status status) throws JSONException, BaseXException {
        String string = TwitterObjectFactory.getRawJSON(status);
        JSONObject json = new JSONObject(string);
        String language = json.getString("lang");
        log.fine(language);
        return json;
    }

}

The JSONObject from Twitter4J cannot just get jammed into XML?

There are a number of online converters which purport to accomplish this, and, which, at least at first glance, seem quite adequate.

see also:

Converting JSON to XML in Java

Java implementation of JSON to XML conversion


回答1:


Use the (excellent) JSON-Java library from json.org then

JSONObject json = new JSONObject(str);
String xml = XML.toString(json);

toString can take a second argument to provide the name of the XML root node.

This library is also able to convert XML to JSON using XML.toJSONObject(java.lang.String string)

Check the Javadoc for more information



来源:https://stackoverflow.com/questions/60015101/how-to-convert-arbitrary-json-to-xml-using-basex

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