Code:
package Utilities;
import Models.AccountModel;
import Models.AccountTableModel;
import com.mycila.xmltool.XMLDoc;
import com.mycila.xmltool.XMLTag;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.JTable;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
/**
*
* @author 8x-soft
*/
public class XMLHelper {
private final static String fileName = "data/Accounts.xml";
public static void writeJtableAccountToXML(JTable table) {
XMLTag doc = XMLDoc.newDocument(true).addRoot("Users");
for (int i = 0; i < table.getRowCount(); i++) {
if (Validator.validateEmail(table.getValueAt(i, 1).toString())) {
doc.addTag("User");
doc.addTag("DisplayName").addText(table.getValueAt(i, 0).toString());
doc.addTag("Email").addText(table.getValueAt(i, 1).toString());
doc.addTag("Password").addText(StringUtil.encryptPassword(table.getValueAt(i, 2).toString()));
doc.addTag("IncomingProtocol").addText(table.getValueAt(i, 3).toString());
doc.addTag("IncomingHost").addText(table.getValueAt(i, 4).toString());
doc.addTag("IncomingPort").addText(table.getValueAt(i, 5).toString());
doc.addTag("IncomingEncrypt").addText(table.getValueAt(i, 6).toString());
doc.addTag("OutgoingProtocol").addText(table.getValueAt(i, 7).toString());
doc.addTag("OutgoingHost").addText(table.getValueAt(i, 8).toString());
doc.addTag("OutgoingPort").addText(table.getValueAt(i, 9).toString());
doc.addTag("OutgoingEncrypt").addText(table.getValueAt(i, 10).toString());
doc.addTag("LoginName").addText(table.getValueAt(i, 11).toString());
doc.gotoParent();
}
}
try {
FileOutputStream fos = new FileOutputStream(fileName);
try {
fos.write(doc.toBytes());
} catch (IOException ex) {
}
} catch (FileNotFoundException ex) {
}
}
public static void loadXMLAccountToAccountTableModel(AccountTableModel model) {
File file = new File(fileName);
if (!file.exists()) return;
XMLTag doc = XMLDoc.from((file), true);
for (int i = 0; i < doc.getChildCount(); i++) {
doc.gotoChild(i+1);
AccountModel account = new AccountModel();
for (int j = 0; j < doc.getChildCount(); j++) {
doc.gotoChild(j+1);
switch (j) {
case 0:
account.setDisplayName(doc.getCurrentTag().getTextContent());
break;
case 1:
account.setEmail(doc.getCurrentTag().getTextContent());
break;
case 2:
account.setPassword(StringUtil.decryptPassword(doc.getCurrentTag().getTextContent()));
break;
case 3:
account.setIncomingProtocol(doc.getCurrentTag().getTextContent());
break;
case 4:
account.setIncomingHost(doc.getCurrentTag().getTextContent());
break;
case 5:
account.setIncomingPort(doc.getCurrentTag().getTextContent());
break;
case 6:
account.setIncomingEncrypt(doc.getCurrentTag().getTextContent());
break;
case 7:
account.setOutgoingProtocol(doc.getCurrentTag().getTextContent());
break;
case 8:
account.setOutgoingHost(doc.getCurrentTag().getTextContent());
break;
case 9:
account.setOutgoingPort(doc.getCurrentTag().getTextContent());
break;
case 10:
account.setOutgoingEncrypt(doc.getCurrentTag().getTextContent());
break;
case 11:
account.setLoginName(doc.getCurrentTag().getTextContent());
break;
}
doc.gotoParent();
}
model.addAccount(account);
doc.gotoParent();
}
}
public static void loadXMLAccountToJtree(JTree tree) {
DefaultTreeModel accountmodel = new DefaultTreeModel(new DefaultMutableTreeNode("Tài khoản"));
tree.setModel(accountmodel);
DefaultMutableTreeNode rootNode = (DefaultMutableTreeNode) accountmodel.getRoot();
File file = new File(fileName);
if (!file.exists()) return;
XMLTag doc = XMLDoc.from((file), true);
for (int i = 0; i < doc.getChildCount(); i++) {
doc.gotoChild(i + 1);
doc.gotoChild(2);
rootNode.add(new DefaultMutableTreeNode(doc.getText()));
doc.gotoParent();
doc.gotoParent();
}
}
public static AccountTableModel getAccountsFromXML() {
File file = new File(fileName);
if (!file.exists()) return null;
AccountTableModel model = new AccountTableModel();
XMLTag doc = XMLDoc.from((file), true);
for (int i = 0; i < doc.getChildCount(); i++) {
doc.gotoChild(i+1);
AccountModel account = new AccountModel();
for (int j = 0; j < doc.getChildCount(); j++) {
doc.gotoChild(j+1);
switch (j) {
case 0:
account.setDisplayName(doc.getCurrentTag().getTextContent());
break;
case 1:
account.setEmail(doc.getCurrentTag().getTextContent());
break;
case 2:
account.setPassword(StringUtil.decryptPassword(doc.getCurrentTag().getTextContent()));
break;
}
doc.gotoParent();
}
model.addAccount(account);
doc.gotoParent();
}
return model;
}
}