/*這個程序完成的功能是讀取company.XML文件的內容,然後
* 將employee標簽下的信息封裝到Employee對象中,最後將
* Employee對象保存到HashMap中
*/
package coresun.cn;
import Javax.XML.parsers.*;
import org.w3c.dom.*;
import org.XML.sax.SAXException;
import Java.io.*;
import Java.util.*;
public class DOMEmployee1 {
public static void main(String[] args) {
HashMap<String, Employee> map = new HashMap<String, Employee>();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = factory.newDocumentBuilder();
//解析company.XML文件,並且獲得文檔節點
Document document = db.parse(new File("company.XML"));
//通過文檔節點獲得,獲取根元素節點
Element root = document.getDocumentElement();
//獲取employee節點的集合
NodeList nl = root.getElementsByTagName("employee");
for (int i = 0; i < nl.getLength(); i++) {
Employee e = new Employee();
//獲取employee元素
Element n = (Element)nl.item(i);
//獲取employee元素的屬性值
String id = n.getAttribute("id");
String sex = n.getAttribute("sex");
//設置employee的id和sex屬性
e.setId(id);
e.setSex(sex);
//獲取name元素的值
Element name = (Element)(n.getElementsByTagName("name").item(0));
Text nameText = (Text)name.getChildNodes().item(0);
e.setName(nameText.getData().trim());
//獲取age元素的值
Element age = (Element)(n.getElementsByTagName("age").item(0));
Text ageText = (Text)age.getChildNodes().item(0);
String sAge = ageText.getData().trim();
e.setAge(Integer.parseInt(sAge));
//獲取interest元素的值
NodeList interestList = n.getElementsByTagName("interest");
//如果不存在interest元素則interestList長度為0
String[] interestStringArray = new String[interestList.getLength()];
for (int j = 0; j < interestList.getLength(); j++) {
Element in = (Element)interestList.item(j);
Text interestText = (Text)in.getChildNodes().item(0);
interestStringArray[j] = interestText.getData();
}
e.setInterest(interestStringArray);
//獲取salary元素的值
NodeList salaryList = n.getElementsByTagName("salary");
Element salaryElement = (Element)salaryList.item(0);
NodeList salaryChild = salaryElement.getChildNodes();
for (int j = 0; j < salaryChild.getLength(); j++) {
Node nSalaryChild = salaryChild.item(j);
if(nSalaryChild instanceof Element){
Text salaryText = (Text)nSalaryChild.getChildNodes().item(0);
String sSalry = salaryText.getData();
e.setSalary(Float.parseFloat(sSalry));
}
}
//以ID為key添加到HashMap中
map.put(id, e);
}
System.out.println(map);
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Employee類源代碼如下:
package coresun.cn;
public class Employee {
private String id;
private String sex;
private String name;
private int age;
private float salary;
private String[] interest;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public float getSalary() {
return salary;
}
public void setSalary(float salary) {
this.salary = salary;
}
public String[] getInterest() {
return interest;
}
public void setInterest(String[] interest) {
this.interest = interest;
}
//重寫Object類的toString()方法
public String toString() {
return "id="+this.id+" sex="+this.sex+" name"+this.name+
" salary"+this.salary+" interest"+this.interest;
}
}