java解析xml文件 (二)

2014-11-24 10:41:14 · 作者: · 浏览: 1
tln(dom.getMessage());
System.exit(1);
} catch (IOException ioe) {
System.err.println(ioe);
System.exit(1);
}
//获得根节点StuInfo
Element elmtStuInfo = doc.getDocumentElement();
//得到所有student节点
NodeList nlStudent = elmtStuInfo.getElementsByTagNameNS(
strNamespace, "student");
for (……){
//当前student节点元素
Element elmtStudent = (Element)nlStudent.item(i);
NodeList nlCurrent = elmtStudent.getElementsByTagNameNS(
strNamespace, "name");
}对于读取得方法其实是很简单的,写入xml文件也是一样不复杂。

Java代码 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
try {
builder = factory .newDocumentBuilder();
} catch (ParserConfigurationException pce) {
System.err.println(pce);
System.exit(1);
}
Document doc = null;
doc = builder .newDocument();
//下面是建立XML文档内容的过程,
//先建立根元素"学生花名册"
Element root = doc.createElement("学生花名册");
//根元素添加上文档
doc.appendChild(root);
//建立"学生"元素,添加到根元素
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);
[java] view plaincopyprint DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
try {
builder = factory .newDocumentBuilder();
} catch (ParserConfigurationException pce) {

System.err.println(pce);
System.exit(1);
}
Document doc = null;
doc = builder .newDocument();
//下面是建立XML文档内容的过程,
//先建立根元素"学生花名册"
Element root = doc.createElement("学生花名册");
//根元素添加上文档
doc.appendChild(root);
//建立"学生"元素,添加到根元素
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);

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
try {
builder = factory .newDocumentBuilder();
} catch (ParserConfigurationException pce) {
System.err.println(pce);
System.exit(1);
}
Document doc = null;
doc = builder .newDocument();
//下面是建立XML文档内容的过程,
//先建立根元素"学生花名册"
Element root = doc.createElement("学生花名册");
//根元素添加上文档
doc.appendChild(root);
//建立"学生"元素,添加到根元素
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.crea