Chương trình java sau đây ứng dụng mô hình DOM để duyệt qua 1 văn bản XML sau đó hiển thị lên JTree.
Trong đó lớp MyNode.java có nội dung sau
package dom.swing;
import java.util.ArrayList;
public class MyNode {
private String nodeID;//ID
private String nodeValue;//giá trị vủa node
private ArrayList<String>attributes;//lưu giữ các thuộc tính của node
public String getNodeID() {
return nodeID;
}
public void setNodeID(String nodeID) {
this.nodeID = nodeID;
}
public String getNodeValue() {
return nodeValue;
}
public void setNodeValue(String nodeValue) {
this.nodeValue = nodeValue;
}
public ArrayList<String> getAttributes() {
return attributes;
}
public void setAttributes(ArrayList<String> attributes) {
this.attributes = attributes;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((nodeID == null) ? 0 : nodeID.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final MyNode other = (MyNode) obj;
if (nodeID == null) {
if (other.nodeID != null)
return false;
} else if (!nodeID.equals(other.nodeID))
return false;
return true;
}
public MyNode(String nodeID, String nodeValue, ArrayList<String> attributes) {
super();
this.nodeID = nodeID;
this.nodeValue = nodeValue;
this.attributes = attributes;
}
public MyNode() {
super();
}
@Override
public String toString() {
return this.nodeID;
}
}