【问题标题】:how to convert arbitrary JSON to XML using BaseX?如何使用 BaseX 将任意 JSON 转换为 XML?
【发布时间】:2020-05-17 18:50:23
【问题描述】:

任意JSON如何使用BaseX转换to任意XML

我正在从BaseX 中查看JsonParser 以获得此specific 解决方案。

在这种情况下,我 have 使用 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;
    }

}

来自Twitter4JJSONObject 不能直接卡在XML 中吗?

有许多 online converters 声称可以实现这一点,而且,至少乍一看,这似乎足够了。

另见:

Converting JSON to XML in Java

Java implementation of JSON to XML conversion

【问题讨论】:

    标签: java json xml converters basex


    【解决方案1】:

    然后使用来自 json.org 的(优秀的)JSON-Java 库

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

    toString 可以使用第二个参数来提供 XML 根节点的名称。

    这个库还可以使用XML.toJSONObject(java.lang.String string)将XML转换成JSON

    查看Javadoc了解更多信息

    【讨论】:

    猜你喜欢
    • 2020-10-20
    • 1970-01-01
    • 2016-01-02
    • 1970-01-01
    • 2015-08-30
    • 2011-04-08
    • 2012-10-12
    • 2022-01-21
    • 2021-07-22
    相关资源
    最近更新 更多