them,xoa,sua data trong xml

5

Click here to load reader

Upload: nguyen-linh

Post on 22-Jun-2015

804 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Them,xoa,sua data trong xml

1. Insert accountNếu chúng ta chưa có file XML thì đoạn code sau đây sẽ tự tạo ra file XML và insert dữ liệu vào, nếu có file XML rồi thì sẽ insert thêm dữ liệu vào sau record cuối cùng có trong file.A.Tạo dữ liệu XML Text Content:

public static File f = new File("XMLData/user.xml");

public static String username;

public static String password;

public static String role;

private Document insert() throws Exception {

Document doc = null;

Element root = null;

if (!f.exists()) {

doc = DocumentBuilderFactory.newInstance().

newDocumentBuilder().newDocument();

root = doc.createElement("users");

doc.appendChild(root);

} else {

doc = DocumentBuilderFactory.newInstance().

newDocumentBuilder().parse(f);

root = doc.getDocumentElement();

}

Element user = doc.createElement("user");

root.appendChild(user);

Element name = doc.createElement("username");

Text userText = doc.createTextNode(username);

name.appendChild(userText);

user.appendChild(name);

Element pass = doc.createElement("password");

Text passText = doc.createTextNode(password);

pass.appendChild(passText);

user.appendChild(pass);

Element rol = doc.createElement("role");

Text roleText = doc.createTextNode(role);

rol.appendChild(roleText);

user.appendChild(rol);

return doc;

Page 2: Them,xoa,sua data trong xml

}

B.Tạo dữ liệu XML có Attributes:

static File f = new File("XMLData/users.xml");

static String username;

static String password;

static String role;

private Document insertAccount()

throws Exception {

Document doc;

Element root;

if (!f.exists()) {

doc = DocumentBuilderFactory.newInstance().

newDocumentBuilder().newDocument();

root = doc.createElement("users");

doc.appendChild(root);

} else {

doc = DocumentBuilderFactory.newInstance().

newDocumentBuilder().parse(f);

root = doc.getDocumentElement();

}

Element user = doc.createElement("user");

user.setAttribute("username", username);

user.setAttribute("password", password);

user.setAttribute("role", role);

root.appendChild(user);

return doc;

}

2. Update accountA.Tạo dữ liệu XML Text Content:

public static File f = new File("XMLData/user.xml");

public static String username;

public static String password;

public static String role;

private void searchAndModify(Node node) {

if (node == null) {

return;

}

Page 3: Them,xoa,sua data trong xml

if (node.getNodeName().equals("user")) {

NodeList list = node.getChildNodes();

for (int index = 0; index < list.getLength(); index++) {

if (list.item(index).getNodeName().equals("username")) {

if (list.item(index).getTextContent().equals(username)) {

list.item(index + 1).setTextContent(password);

list.item(index + 2).setTextContent(role);

return;

}

}

}

}

NodeList children = node.getChildNodes();

for (int index = 0; index < children.getLength(); index++) {

searchAndModify(children.item(index));

}

}

B.Tạo dữ liệu XML có Attributes:

static File f = new File("XMLData/users.xml");

static String username;

static String password;

static String role;

private void seachAndUpdate(Node node) {

if (node == null) {

return;

}

if (node.getNodeName().equals("user")) {

if (node.getAttributes().getNamedItem("username").

getNodeValue().equals(username)) {

node.getAttributes().getNamedItem("password").

setNodeValue(password);

node.getAttributes().getNamedItem("role").setNodeValue(role);

return;

}

}

NodeList children = node.getChildNodes();

for (int index = 0; index < children.getLength(); index++) {

Page 4: Them,xoa,sua data trong xml

seachAndUpdate(children.item(index));

}

}

3. Delete accountA.Tạo dữ liệu XML Text Content:

public static File f = new File("XMLData/user.xml");

public static String username;

public static String password;

public static String role;

private void searchAndDelete(Node node) {

if (node == null) {

return;

}

if (node.getNodeName().equals("user")) {

NodeList list = node.getChildNodes();

for (int index = 0; index < list.getLength(); index++) {

if (list.item(index).getNodeName().equals("username")) {

if (list.item(index).getTextContent().equals(username)) {

node.getParentNode().removeChild(node);

return;

}

}

}

}

NodeList children = node.getChildNodes();

for (int index = 0; index < children.getLength(); index++) {

searchAndDelete(children.item(index));

}

}

B.Tạo dữ liệu XML có Attributes:

static File f = new File("XMLData/users.xml");

static String username;

static String password;

static String role;

private void searchAndDelete(Node node) {

if (node == null) {

return;

Page 5: Them,xoa,sua data trong xml

}

if (node.getNodeName().equals("user")) {

if (node.getAttributes().getNamedItem("username").

getNodeValue().equals(username)) {

node.getParentNode().removeChild(node);

System.out.println(username);

return;

}

}

NodeList children = node.getChildNodes();

for (int index = 0; index < children.getLength(); index++) {

searchAndDelete(children.item(index));

}

}

4. Ghi tất source ra file XMLTôi chia sẻ cho các bạn 3 hàm để insert update delete dữ liệu của file XML. 3 hàm đó chỉ hoạt động trên cây DOM nằm trong bộ nhớ chứ không tác động vào file XML của chúng ta. Nếu các bạn muốn tất cả dữ liệu chúng ta đã thao tác ở 3 hàm trên thì sau khi gọi 3 hàm trên sử dụng xong thì các bạn phải gọi thêm 1 hàm nữa, để ghi toàn bộ cây DOM ra file XML. Cụ thể hàm ghi như sau:

private static void writeXML(Document doc) {

try {

Source source = new DOMSource(doc);

Result result = new StreamResult(f);

Transformer trans = TransformerFactory.newInstance().

newTransformer();

trans.transform(source, result);

} catch (Exception ex) {

ex.printStackTrace();

}

}