私はJooxを使用することをお勧めします。流暢なapiはjqueryのようなスタイルでdomを管理します。依存関係はありません。あなたはmavenでインストールできます。
Javaのパフォーマンスと同じように、要素*(coleagesの経験)よりも属性を使用する方が良いです。
だから私は、あなたがこの方法であなたのモデルを使用することをお勧め:私はあなたのためJooxを使用して、このクラスを作成し
<items>
<item id="1" path="path"/>
...
</items>
:
package test;
import static org.joox.JOOX.$;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import org.apache.commons.io.FileUtils;
import org.joox.JOOX;
import org.joox.Match;
import org.xml.sax.SAXException;
public class XmlDBTable{
private Match dbMatch;
private String xmlBeforeCommit;
private boolean building;
private String root;
private String item;
private String itemXpathTemplate;
private File file;
public void setRoot(String root){
this.root=root;
}
public void setItemTemplate(String item){
this.item=item;
}
public void setItemXpathTemplate(String itemXpath){
this.itemXpathTemplate=itemXpath;
}
public void setFile(String path){
file=new File(path);
if(file.exists())
try {
dbMatch=$(file);
} catch (SAXException | IOException e) {
e.printStackTrace();
}
}
public Match xml() {
Match dbMatch= dbMatch();
building=true;
return dbMatch;
}
public void insertOrUpdate(String keyEqualsValueList){
Match dbMatch= dbMatch();
String newItem=item;
if(keyEqualsValueList.contains(",")) {
String[] keyValues=keyEqualsValueList.split(",");
for(String keyValue:keyValues) {
String key=keyValue.split("=")[0].toUpperCase();
String value=keyValue.split("=")[1];
newItem=newItem.replace(key,value);
}
}else {
String key=keyEqualsValueList.split("=")[0].toUpperCase();
String value=keyEqualsValueList.split("=")[1];
newItem=newItem.replace(key,value);
}
String id=itemId(newItem);
String itemXpath=itemXpath(id);
Match item=$(dbMatch).xpath(itemXpath);
if ($(item).isEmpty()){
$(dbMatch).append(newItem);
}else{
if(keyEqualsValueList.contains(",")) {
String[] keyValues=keyEqualsValueList.split(",");
for(String keyValue:keyValues) {
String key=keyValue.split("=")[0];
String value=keyValue.split("=")[1];
$(dbMatch).xpath(itemXpath).attr(key,value);
}
}else {
String key=keyEqualsValueList.split("=")[0].toUpperCase();
String value=keyEqualsValueList.split("=")[1];
$(dbMatch).xpath(itemXpath).attr(key,value);
}
}
building=true;
System.out.println("Item id "+id+" added ok");
}
public void delete(String id){
Match dbMatch= dbMatch();
String itemXpath=itemXpath(id);
Match item=$(dbMatch).xpath(itemXpath);
if (!$(item).isEmpty()){
$(dbMatch).xpath(itemXpath).remove();
System.out.println("Item id "+id+" deleted ok");
}else{
System.out.println("The item didn't exist");
}
building=true;
}
private String itemId(String item) {
String id=xmlStrToMatch(item, "UTF-8").attr("id");
return id;
}
private String itemXpath(String id) {
String itemXpath=itemXpathTemplate.replace("ID", id);
return itemXpath;
}
public synchronized boolean commit(){
try {
FileUtils.writeStringToFile(file,dbMatch().toString(),"UTF-8");
building=false;
} catch (IOException e) {
e.printStackTrace();
}
return true;
}
public boolean rollBack(){
dbMatch();
dbMatch=xmlStrToMatch(xmlBeforeCommit, "UTF-8");
System.out.println("Removed all changes after last commit");
building=false;
return true;
}
private Match dbMatch(){
if(dbMatch==null){
dbMatch=xmlStrToMatch(root,"UTF-8");
}
if(!building){
if(xmlBeforeCommit!=null){
xmlBeforeCommit=dbMatch.toString();
}else {
xmlBeforeCommit=root;
}
}
return dbMatch;
}
private static Match xmlStrToMatch(String xmlString, String encoding){
Match match = null;
try {
match = $(JOOX.builder().parse(new ByteArrayInputStream(xmlString.getBytes(encoding))));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return match;
}
}
を、それを使用するには、使用することができます。
package test;
public class ItemsTable{
public static void main(String... args){
XmlDBTable itemsTable= new XmlDBTable();
itemsTable.setRoot("<items></items>");
itemsTable.setItemTemplate("<item id=\"ID\" path=\"PATH\"/>");
itemsTable.setItemXpathTemplate("/items/item[@id='ID']");
itemsTable.setFile("C:\\users\\jesus\\Downloads\\temp\\itemsTable.xml");
itemsTable.insertOrUpdate("id=1,path=myPath");
itemsTable.insertOrUpdate("id=2,path=myPath2");
itemsTable.delete("2");
itemsTable.insertOrUpdate("id=3,path=myPath3");
//save changes
itemsTable.commit();
//you can use all power of Joox Methods too to do the same or more
//Select path from item with id=1
String path1=itemsTable.xml().xpath("//item[@id='1']").attr("path");
//print it
System.out.println(path1);
//Add subItem id=0 to item with id=1
itemsTable.xml().xpath("//item[@id='1']").append("<subitem id=\"0\" color=\"black\">Super Subitem</subitem>");
//print the text of subitem recently added
String subitem1=itemsTable.xml().xpath("//subitem[@id='0']").text();
System.out.println(subitem1);
//save changes
itemsTable.commit();
//Add subItem id=1 to item with id=1
itemsTable.xml().xpath("//item[@id='1']").append("<subitem id=\"1\" color=\"blue\">Super Subitem</subitem>");
//rollback changes after last commit (subitem id=1 in item id=1 deleted)
itemsTable.rollBack();
itemsTable.insertOrUpdate("id=4,path=myPath4");
//save changes
itemsTable.commit();
}
}
※文字列テンプレートでは、デフォルト値として大文字を使用しています。 id = "ID" * insertOrUpdateとdeleteの別のメソッドは、属性として列としてのみ機能します。 * Jooxメソッドを使用して、次のように新しい行を挿入できます。
これは、ファイル生成されます。これは、コンソールで出力され
<items>
<item id="1" path="myPath">
<subitem color="black" id="0">Super Subitem</subitem>
</item>
<item id="3" path="myPath3"/>
<item id="4" path="myPath4"/>
</items>
:
Item id 2 added ok
Item id 2 deleted ok
Item id 3 added ok
myPath
Super Subitem
Removed all changes after last commit
Item id 4 added ok
私のMavenの依存関係:
<dependency>
<groupId>org.jooq</groupId>
<artifactId>joox</artifactId>
<version>1.4.0</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
</dependency>
あなたはデータベースのようなものが必要な場合は、使用データベース。あなたは四角い車輪を再発明しています。 – fiction
はいMySQLではなくmysqlのようなデータベースを使用する方が簡単ですが、この場合のようにXMLをデータベースとして使用することもできます。 –
* *使用できます。しかし、なぜあなたはすべきですか?いくつかのトップノッチDBMSがあります。これは、これまで以上に100倍のパフォーマンスとスケーラビリティを実現し、より少ないコードを記述する必要があります。もちろん、XMLはデータベースフォーマットのための恐ろしい選択です(DB間のシリアライゼーション/交換のために、それは良いことですが、このためではありません)。 – delnan