简体   繁体   English

如何使用 BaseX 将任意 JSON 转换为 XML?

[英]how to convert arbitrary JSON to XML using BaseX?

How is arbitrary JSON converted to arbitrary XML using BaseX ?如何使用BaseX将任意JSON转换任意XML

I'm looking at JsonParser from BaseX for this specific solution.我正在查看BaseX JsonParser以获取此特定解决方案。

In this case, I have tweets using Twitter4J :在这种情况下,我使用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 ?来自Twitter4JJSONObject不能只是塞进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 中将 JSON 转换为 XML

Java implementation of JSON to XML conversion JSON 到 XML 转换的 Java 实现

Use the (excellent) JSON-Java library from json.org then然后使用来自 json.org 的(优秀的)JSON-Java 库

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. toString可以采用第二个参数来提供 XML 根节点的名称。

This library is also able to convert XML to JSON using XML.toJSONObject(java.lang.String string)该库还能够使用XML.toJSONObject(java.lang.String string)将 XML 转换为 JSON

Check the Javadoc for more information查看Javadoc以获取更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM