input.xml檔案
<?xml version="1.0" encoding="GB2312"?>
<學生花名冊>
<學生 性別 = "男">
<姓名>李華</姓名>
<年齡>14</年齡>
<電話>6287555</電話>
</學生>
<學生 性別 = "男">
<姓名>張三</姓名>
<年齡>16</年齡>
<電話>8273425</電話>
</學生>
</學生花名冊>
StudentBean .java
package xmlTest;
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class StudentBean {
private String sex;
private String name;
private int age;
private String phone;
public StudentBean() {
}
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 String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
XMLTest.java
package xmlTest;
import java.io.*;
//Java基礎包,包含各種IO操作
import java.util.*;
//Java基礎包,包含各種標準資料結構操作
import javax.xml.parsers.*;
//XML解析器介面
import org.w3c.dom.*;
//XML的DOM實現
import org.apache.crimson.tree.XmlDocument;
//寫XML檔案要用到
/**
* <p>Title: </p>
* <p>Description: </p>
* <p>Copyright: Copyright (c) 2005</p>
* <p>Company: </p>
* @author not attributable
* @version 1.0
*/
public class XMLTest {
public XMLTest() {
}
private Vector student_Vector;
//讀取xml檔案
private void readXMLFile(String inFile) throws Exception {
//為解析XML作準備,
//建立DocumentBuilderFactory執行個體,指定DocumentBuilder
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = null;
try {
db = dbf.newDocumentBuilder();
}
catch (ParserConfigurationException pce) {//出異常時輸出異常資訊,然後退出,下同
System.err.println(pce);
System.exit(1);
}
Document doc = null;
try {
doc = db.parse(inFile);
}
catch (DOMException dom) {
System.err.println(dom.getMessage());
System.exit(1);
}
catch (IOException ioe) {
System.err.println(ioe);
System.exit(1);
}
//下面是解析XML的全過程,
//比較簡單,先取根項目"學生花名冊"
Element root = doc.getDocumentElement();
//取"學生"元素列表
NodeList students = root.getElementsByTagName("學生");
for (int i = 0; i < students.getLength(); i++) {
//依次取每個"學生"元素
Element student = (Element) students.item(i);
//建立一個學生的Bean執行個體
StudentBean studentBean = new StudentBean();
//取學生的性別屬性
studentBean.setSex(student.getAttribute("性別"));
//取"姓名"元素,下面類同
NodeList names = student.getElementsByTagName("姓名");
if (names.getLength() == 1) {
Element e = (Element) names.item(0);
Text t = (Text) e.getFirstChild();
studentBean.setName(t.getNodeValue());
}
NodeList ages = student.getElementsByTagName("年齡");
if (ages.getLength() == 1) {
Element e = (Element) ages.item(0);
Text t = (Text) e.getFirstChild();
studentBean.setAge(Integer.parseInt(t.getNodeValue()));
}
NodeList phones = student.getElementsByTagName("電話");
if (phones.getLength() == 1) {
Element e = (Element) phones.item(0);
Text t = (Text) e.getFirstChild();
studentBean.setPhone(t.getNodeValue());
}
student_Vector.add(studentBean);
}
}
//寫xml檔案
private void writeXMLFile(String outFile) throws Exception {
//為解析XML作準備,
//建立DocumentBuilderFactory執行個體,指定DocumentBuilder
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = null;
try {
db = dbf.newDocumentBuilder();
}
catch (ParserConfigurationException pce) {
System.err.println(pce);
System.exit(1);
}
Document doc = null;
doc = db.newDocument();
//下面是建立XML文檔內容的過程,
//先建立根項目"學生花名冊"
Element root = doc.createElement("學生花名冊");
//根項目添加上文檔
doc.appendChild(root);
//取學生資訊的Bean列表
for (int i = 0; i < student_Vector.size(); i++) {
//依次取每個學生的資訊
StudentBean studentBean = (StudentBean) student_Vector.get(i);
//建立"學生"元素,添加到根項目
Element student = doc.createElement("學生");
student.setAttribute("性別", studentBean.getSex());
root.appendChild(student);
//建立"姓名"元素,添加到學生下面,下同
Element name = doc.createElement("姓名");
student.appendChild(name);
Text tName = doc.createTextNode(studentBean.getName());
name.appendChild(tName);
Element age = doc.createElement("年齡");
student.appendChild(age);
Text tAge = doc.createTextNode(String.valueOf(studentBean.getAge()));
age.appendChild(tAge);
Element phone = doc.createElement("電話");
student.appendChild(phone);
Text tPhone = doc.createTextNode(studentBean.getPhone());
phone.appendChild(tPhone);
}
//把XML文檔輸出到指定的檔案
FileOutputStream outStream = new FileOutputStream(outFile);
OutputStreamWriter outWriter = new OutputStreamWriter(outStream);
( (XmlDocument) doc).write(outWriter, "GB2312");
outWriter.close();
outStream.close();
}
private void writeHTMLFile(String outFile) throws Exception {
StringBuffer HTMLString=new StringBuffer();
HTMLString.append("<html>/n<head>/n<title>學生花名冊</title>/n<meta http-equiv=/"Content-Type/" content=/"text/html; charset=gb2312/">/n</head>/n<body>/n");
if(student_Vector.size()>0){
HTMLString.append("<table width=100% border=1 cellpadding=5 cellspacing=0>/n");
HTMLString.append("/t<th colspan=4>學生花名冊</th>/n");
HTMLString.append("/t<tr align=center>/n");
HTMLString.append("/t/t<td>姓名</td>/n");
HTMLString.append("/t/t<td>性別</td>/n");
HTMLString.append("/t/t<td>年齡</td>/n");
HTMLString.append("/t/t<td>電話</td>/n");
HTMLString.append("/t</tr>/n");
for (int i = 0; i < student_Vector.size(); i++) {
StudentBean studentBean = (StudentBean) student_Vector.get(i);
HTMLString.append("/t<tr align=center>/n");
HTMLString.append("/t/t<td>"+studentBean.getName()+"</td>/n");
HTMLString.append("/t/t<td>"+studentBean.getSex()+"</td>/n");
HTMLString.append("/t/t<td>"+studentBean.getAge()+"</td>/n");
HTMLString.append("/t/t<td>"+studentBean.getPhone()+"</td>/n");
HTMLString.append("/t</tr>/n");
}
HTMLString.append("</table>");
}
HTMLString.append("/n</body>/n</html>");
FileOutputStream outStream = new FileOutputStream(outFile);
OutputStreamWriter outWriter = new OutputStreamWriter(outStream);
outWriter.write(HTMLString.toString());
outWriter.close();
outStream.close();
}
//最後加入測試主函數,如下:
public static void main(String[] args) throws Exception {
//建立測試執行個體
XMLTest xmlTest = new XMLTest();
//初始化向量列表
xmlTest.student_Vector = new Vector();
System.out.println("開始讀Input.xml檔案");
xmlTest.readXMLFile("file/xml/Input.xml");
System.out.println("讀入完畢");
//System.out.println("開始寫Output.xml檔案");
//xmlTest.writeXMLFile("file/xml/Output.xml");
System.out.println("開始寫Output.htm檔案");
xmlTest.writeHTMLFile("file/xml/Output.htm");
System.out.println("寫入完成");
}
}