如何使用BaseX将任意JSON转换为XML? [英] how to convert arbitrary JSON to XML using BaseX?
问题描述
任意 JSON
如何转换使用 BaseX
任意 XML
?
我正在从 BaseX
中查看 JsonParser
https://github.com/BaseXdb/basex/blob/master/basex-core/src/main/java/org/basex/io/parse/json/JsonParser.java rel = nofollow noreferrer>特定解决方案。
I'm looking at JsonParser
from BaseX
for this specific solution.
在这种情况下,我有使用 Twitter4J
进行鸣叫:
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;
}
}
来自
不能仅仅卡在 Twitter4J
的JSONObject XML
中吗?
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.
另请参见:
推荐答案
使用json.org中的(优秀)JSON-Java库,然后
Use the (excellent) JSON-Java library from json.org then
JSONObject json = new JSONObject(str);
String xml = XML.toString(json);
toString
可以接受第二个参数来提供XML根节点的名称。
toString
can take a second argument to provide the name of the XML root node.
该库还能够使用 XML.toJSONObject(java.lang.String string )
检查 Javadoc 了解更多信息
这篇关于如何使用BaseX将任意JSON转换为XML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!