java - 如何将 XML 字符串添加到现有的 BaseX 数据库?
问题描述
使用org.json
并且,至关重要的是,JSONArray
而不是JSONObject
,设法,似乎至少,XML
通过Twitter4J
. 但是,它显示为:
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
假设这BaseX
会引起XML
足够的争论,我希望将文档添加到数据库中:
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
字符串一起使用,这可能是正在传递的内容。这是添加到数据库的正确方法吗?XML
Java 代码:
package basex;
import java.net.MalformedURLException;
import java.net.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
文件:
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 https://www.w3schools.com/xml/books.xml
db 1 101838
twitter 15 5203
w3school_data 1 5209 https://www.w3schools.com/xml/note.xml
4 database(s).
>
> drop database twitter
Database 'twitter' was dropped.
>
> list
Name Resources Size Input Path
-------------------------------------------------------------------------------
com.w3schools.books 1 6290 https://www.w3schools.com/xml/books.xml
db 1 101838
w3school_data 1 5209 https://www.w3schools.com/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 https://www.w3schools.com/xml/books.xml
db 1 101838
twitter 15 5203
w3school_data 1 5209 https://www.w3schools.com/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);
}
推荐阅读
- javascript - 使间隙大小与系列数据中的间隙成比例
- java - InputStream - 标记、跳过、读取、重置
- python - 无法使用 Python 从 Google 地球引擎脚本中获取结果
- asp.net - 如何以编程方式访问默认的 ASP.NET/IIS 错误描述?
- c++ - OpenCV 错误:在 cv::cvtColor 中断言失败 (scn == 3 || scn == 4)
- javascript - 扩展 Highcharts 导出
- java - 如何在 javafx 中设置任意 TreeViewTable 扩展器列
- r - 如何通过 dplyr 进行 2 组的 cumsum?
- datatable - 在具有可点击行的 Richfaces 数据表中,我希望一列忽略 onClickEvent
- node.js - Vue:使用 vue-cli 将请求代理到单独的后端服务器