[TUT] Dùng DOM Thao Tác Dữ Liệu XML - MINHBXN

Trong bài này mình sẽ hường dẫn các bạn sử dụng DOM thao tác dữ liệu XML cụ thể ở đây là insert, update, delete accounts của 1 file XML có tên là users.xml

1. Insert account Nế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; }

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 account 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 void searchAndModify(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)) { 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++) { seachAndUpdate(children.item(index)); } }

3. Delete account 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 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; } 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 XML Tô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(); } }

Chúc các bạn thành công

Share this:

  • Facebook
  • X
Like Loading...

Related

Từ khóa » Thao Tác Với File Xml Java