如何将 XML 字符串添加到现有 BaseX 数据库?

编程入门 行业动态 更新时间:2024-10-24 01:56:19
本文介绍了如何将 XML 字符串添加到现有 BaseX 数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

使用 org.json 和至关重要的 JSONArray 而不是 JSONObject,设法norelrefern>norelrefern>norelrefern>norelrefern.但是,它显示为:

Using org.json and, crucially, JSONArray rather than JSONObject, managed to, seemingly at least, get XML output through Twitter4J. However, it shows as:

The Markup In The Document Following The Root Element Must Be Well-formed., Line '1', Column '98'. The Markup In The Document Following The Root Element Must Be Well-formed.

尝试验证时.

但是,它至少具有 XML 的外观.

It does, however, have at least the appearance of XML.

假设 BaseX 将充分处理 XML,我希望将文档添加到数据库中:

Assuming that BaseX will wrangle the XML sufficiently, I'm looking to add the document to a database as:

new Open(databaseName).execute(context); for (int i = 0; i < tweets.length(); i++) { jsonStringTweet = tweets.get(i).toString(); jsonObjectTweet = new org.json.JSONObject(jsonStringTweet); stringXml = XML.toString(jsonObjectTweet); log.info(stringXml); new Add(null, stringXml).execute(context); }

参考:

构造函数详细信息

Add public Add(java.lang.String path) Constructor, specifying a target path. The input needs to be set via Command.setInput(InputStream). Parameters: path - target path, optionally terminated by a new file name Add public Add(java.lang.String path, java.lang.String input) Constructor, specifying a target path and an input. Parameters: path - target path, optionally terminated by a new file name. If null, the name of the input will be set as path. input - input file or XML string

但是,我不太符合要求.输入似乎说它将与 XML 字符串一起使用,这大概是传递的内容.是 coderel> 和 XML 到数据库?

However, I don't quite follow the requirements. The input seems to say that it will work with an XML string, which, presumably is what is being passed. Is that the correct way to add XML to a database?

Java 代码:

package basex; import java.MalformedURLException; import java.URL; 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.JSONArray; import twitter4j.JSONException; 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 list() throws BaseXException { log.fine(new List().execute(context)); } private void drop() throws BaseXException { new Set("parser", parserType).execute(context); new DropDB(databaseName).execute(context); list(); } private void create() throws BaseXException, JSONException { new Set("parser", parserType).execute(context); new CreateDB(databaseName).execute(context); new List().execute(context); list(); } private void add(JSONArray tweets) throws JSONException, BaseXException { long id = 0L; String jsonStringTweet = null; org.json.JSONObject jsonObjectTweet = null; String stringXml = null; new Open(databaseName).execute(context); for (int i = 0; i < tweets.length(); i++) { jsonStringTweet = tweets.get(i).toString(); jsonObjectTweet = new org.json.JSONObject(jsonStringTweet); stringXml = XML.toString(jsonObjectTweet); log.info(stringXml); new Add(null, stringXml).execute(context); } } public void dropCreateAdd(JSONArray tweets) throws MalformedURLException, BaseXException, JSONException { init(); drop(); create(); add(tweets); list(); } }

推荐答案

还算可以,至少可以添加一个 XML 文件:

sorta kinda works, at least can add an XML file:

thufir@dur:~/NetBeansProjects/twitterBaseX$ thufir@dur:~/NetBeansProjects/twitterBaseX$ basex BaseX 9.0.1 [Standalone] Try 'help' to get more information. > > list Name Resources Size Input Path ------------------------------------------------------------------------------- com.w3schools.books 1 6290 www.w3schools/xml/books.xml db 1 101838 twitter 15 5203 w3school_data 1 5209 www.w3schools/xml/note.xml 4 database(s). > > drop database twitter Database 'twitter' was dropped. > > list Name Resources Size Input Path ------------------------------------------------------------------------------- com.w3schools.books 1 6290 www.w3schools/xml/books.xml db 1 101838 w3school_data 1 5209 www.w3schools/xml/note.xml 3 database(s). > > exit Have a nice day. thufir@dur:~/NetBeansProjects/twitterBaseX$ thufir@dur:~/NetBeansProjects/twitterBaseX$ gradle clean run > Task :run /home/thufir/basex/.basex: writing new configuration file. BUILD SUCCESSFUL in 2s 4 actionable tasks: 4 executed thufir@dur:~/NetBeansProjects/twitterBaseX$ thufir@dur:~/NetBeansProjects/twitterBaseX$ basex /home/thufir/basex/.basex: Unknown option 'RESTXQERRORS'. /home/thufir/basex/.basex: writing new configuration file. BaseX 9.0.1 [Standalone] Try 'help' to get more information. > > list Name Resources Size Input Path ------------------------------------------------------------------------------- com.w3schools.books 1 6290 www.w3schools/xml/books.xml db 1 101838 twitter 15 5203 w3school_data 1 5209 www.w3schools/xml/note.xml 4 database(s). > > open twitter Database 'twitter' was opened in 57.09 ms. > > xquery / <root> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> </root> <root> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> </root> <root> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> </root> <root> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> </root> <root> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> </root> <root> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> </root> <root> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> </root> <root> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> </root> <root> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> </root> <root> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> </root> <root> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> </root> <root> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> </root> <root> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> </root> <root> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> </root> <root> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> </root> Query executed in 232.89 ms. >

代码:

new Open(databaseName).execute(context); for (int i = 0; i < tweets.length(); i++) { jsonStringTweet = tweets.get(i).toString(); jsonObjectTweet = new org.json.JSONObject(jsonStringTweet); stringXml = XML.toString(jsonObjectTweet); log.fine(stringXml); new Add(null, "data.xml").execute(context); }

更多推荐

如何将 XML 字符串添加到现有 BaseX 数据库?

本文发布于:2023-10-27 02:59:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1532110.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   如何将   数据库   XML   BaseX

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!