您好,登錄后才能下訂單哦!
import java.util.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import java.io.*;
import org.apache.crimson.tree.XmlDocument;
class Configure{
private ArrayList student_Vector = new ArrayList();
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);
}
}
public 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();
}
public static void main(String[] args) throws Exception
{
Configure cfg = new Configure();
cfg.readXMLFile("s1.xml");
cfg.writeXMLFile("s2.xml");
}
}
class StudentBean {
private String sex; //學生性別
private String name; //學生姓名
private int age; //學生年齡
private String phone; //電話號碼
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
附:
<學生花名冊>
<學生性別 = "男">
<姓名>李華姓名>
<年齡>14年齡>
<電話>6287555電話>
學生>
<學生性別 = "男">
<姓名>張三姓名>
<年齡>16年齡>
<電話>8273425電話>
學生>
學生花名冊>
[@more@]免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。