Он может добавить XML
из файла как:
thufir@dur:~/NetBeansProjects/twitterBaseX$
thufir@dur:~/NetBeansProjects/twitterBaseX$ ll
total 92
drwxr-xr-x 6 thufir thufir 4096 Feb 3 13:50 ./
drwxr-xr-x 22 thufir thufir 4096 Feb 1 16:05 ../
-rw-rw-r-- 1 thufir thufir 1979 Feb 1 21:17 build.gradle
-rw-rw-r-- 1 thufir thufir 219 Feb 3 01:04 data.xml
drwxr-xr-x 8 thufir thufir 4096 Feb 3 13:50 .git/
-rw-r--r-- 1 thufir thufir 427 Feb 1 16:22 .gitignore
drwxrwxr-x 6 thufir thufir 4096 Jan 30 00:26 .gradle/
drwxrwxr-x 3 thufir thufir 4096 Jan 30 00:24 gradle/
-rwxrwxr-x 1 thufir thufir 5916 Jan 30 00:24 gradlew*
-rw-rw-r-- 1 thufir thufir 2941 Jan 30 00:24 gradlew.bat
-rw-r--r-- 1 thufir thufir 35149 Jan 30 00:22 LICENSE
-rw-r--r-- 1 thufir thufir 51 Jan 30 00:22 README.md
-rw-rw-r-- 1 thufir thufir 361 Jan 31 21:55 settings.gradle
drwxrwxr-x 4 thufir thufir 4096 Jan 30 00:24 src/
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
w3school_data 1 5209 https://www.w3schools.com/xml/note.xml
2 database(s).
>
> exit
Enjoy life.
thufir@dur:~/NetBeansProjects/twitterBaseX$
thufir@dur:~/NetBeansProjects/twitterBaseX$ gradle clean run
> Task :run
/home/thufir/basex/.basex: writing new configuration file.
BUILD SUCCESSFUL in 3s
4 actionable tasks: 3 executed, 1 up-to-date
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
twitter 15 73551
w3school_data 1 5209 https://www.w3schools.com/xml/note.xml
3 database(s).
>
> open twitter
Database 'twitter' was opened in 63.63 ms.
>
> xquery /
<root>
<metadata>
<result_type>recent</result_type>
...
<notifications>false</notifications>
</user>
<favorited>false</favorited>
</root>
Query executed in 292.16 ms.
>
> exit
Enjoy life.
thufir@dur:~/NetBeansProjects/twitterBaseX$
source:
package basex;
import java.io.FileWriter;
import java.io.IOException;
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.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;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import org.basex.core.cmd.Add;
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 String wrap(String string) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
stringBuilder.append("<root>");
stringBuilder.append(string);
stringBuilder.append("</root>");
return stringBuilder.toString();
}
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, IOException {
long id = 0L;
String jsonStringTweet = null;
org.json.JSONObject jsonObjectTweet = null;
String stringXml = null;
String fileName = "tweet.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);
stringXml = wrap(stringXml);
write(stringXml,fileName);
String stringFromFile = read(fileName);
log.fine(stringFromFile);
new Add(fileName, stringXml).execute(context);
}
}
public void dropCreateAdd(JSONArray tweets) throws MalformedURLException, BaseXException, JSONException, IOException {
init();
drop();
create();
add(tweets);
list();
}
private void write(String tweet, String fileName) throws IOException {
log.fine(tweet);
FileWriter fileWriter = new FileWriter(fileName);
fileWriter.write(tweet);
fileWriter.close();
}
private String read(String fileName) throws IOException {
String content = new String(Files.readAllBytes(Paths.get(fileName)), StandardCharsets.UTF_8);
return content;
}
}
, но это не показывает, как добавить строку XML
к базы данных.