Java读写XML文件例子2

import java.util.*;

创新互联专注于企业网络营销推广、网站重做改版、双牌网站定制设计、自适应品牌网站建设、H5开发商城开发、集团公司官网建设、成都外贸网站建设公司、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为双牌等各大城市提供网站开发制作服务。

import javax.xml.parsers.*;

import org.w3c.dom.*;

import java.io.*;

import org.apache.crimson.tree.XmlDocument;

class Configure{

private ArrayListstudent_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);

}

}

publicvoid 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

//依次取每个学生的信息

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();

}

publicstaticvoid main(String[] args)throws Exception

{

Configure cfg =new Configure();

cfg.readXMLFile("s1.xml");

cfg.writeXMLFile("s2.xml");

}

}

class StudentBean {

private Stringsex;//学生性别

private Stringname;//学生姓名

privateintage;//学生年龄

private Stringphone;//电话号码

publicint getAge() {

returnage;

}

publicvoid setAge(int age) {

this.age = age;

}

public String getName() {

returnname;

}

publicvoid setName(String name) {

this.name = name;

}

public String getPhone() {

returnphone;

}

publicvoid setPhone(String phone) {

this.phone = phone;

}

public String getSex() {

returnsex;

}

publicvoid setSex(String sex) {

this.sex = sex;

}

}

附:

<学生花名册>

<学生性别 = "男">

<姓名>李华

<年龄>14

<电话>6287555

<学生性别 = "男">

<姓名>张三

<年龄>16

<电话>8273425

[@more@]
当前标题:Java读写XML文件例子2
网页URL:http://azwzsj.com/article/ghcoei.html