日期:2014-05-20  浏览次数:20988 次

xml文件的插入和删除结点
如题,本人初学,自己依样画葫芦写的程序老是出错。所以在这里求一个写入和删除结点的例子,谢谢各位哥哥姐姐。

------解决方案--------------------
下面是XML文件读写的例子,删除NODE我也不懂.希望能给你提供点帮助.
package XMLTest;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.Vector;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;

//为了保存多个学生信息,还得借助一个集合类(并不是单纯意义上的集合,JAVA中的集合是集合框架的概念,包含向量、列表、哈希表等),
//这里采用Vector向量类。定义在XMLTest测试类中,命名为student_Vector。然后定义两个方法readXMLFile和writeXMLFile,实现读写操作。代码如下:

public class XMLTest {

Vector student_Vector;

private void readXMLFile(String inFile) throws Exception {
// 建立一个解析器工厂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的全过程,
// 比较简单,先取根元素 "students "
Element root = doc.getDocumentElement();
// 取 "学生 "元素列表
NodeList students = root.getElementsByTagName( "student ");
for (int i = 0; i < students.getLength(); i++) {
// 依次取每个 "学生 "元素
Element student = (Element) students.item(i);
// 创建一个学生的Bean实例
StudentBean studentBean = new StudentBean();
// 取学生的性别属性
studentBean.setSex(student.getAttribute( "sex "));
// 取 "姓名 "元素,下面类同

System.out.println(student.getAttribute( "sex "));

NodeList names = student.getElementsByTagName( "name ");
if (names.getLength() == 1) {
Element e = (Element) names.item(0);
Text t = (Text) e.getFirstChild();
studentBean.setName(t.getNodeValue());
System.out.println(t.getNodeValue());
}

NodeList ages = student.getElementsByTagName( "age ");
if (ages.getLength() == 1) {
Element e = (Element) ages.item(0);
Text t = (Text) e.getFirstChild();
studentBean.setAge(Integer.parseInt(t.getNodeValue()));
System.out.println(t.getNodeValue());
}

NodeList phones = student.getElementsByTagName( "phonenumber ");
if (phones.getLength() == 1) {
Element e = (Element) phones.item(0);
Text t = (Text) e.getFirstChild();
studentBean.setPhone(t.getNodeValue());
System.out.println(t.getNodeValue());
}

student_Vector.add(studentBean);
}
}

private void writeXMLFile(String outFile) throws Exception {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = null;
try {
db = dbf.newDocumentBuilder();
} catch (ParserConfigurationException pce) {