1. 向该xml文档里添加一个用户,姓名:王五,;location:铁岭;grade:99
2. 给每个学生增加一条信息,性别。
3. 删除第一个学生张三的所有信息。
4. 将第二个学生李四的姓名改为 李四光
5. 给每个学生增加一个ID属性。
exam.xml | Exam.xml |
<?xml version="1.0" encoding="UTF-8" standalone="no"?><exam>
<student examid="222" idcard="111">
<name>张三</name>
<location>沈阳</location>
<grade>89</grade>
</student>
<student examid="444" idcard="333">
<name>李四</name>
<location>大连</location>
<grade>97</grade>
</student>
</exam>
|
<?xml version="1.0" encoding="UTF-8" standalone="no"?><exam>
<student ID="0" examid="444" idcard="333">
<name>李四光</name>
<location>大连</location>
<grade>97</grade>
<性别/></student>
<student ID="1" examid="333" idcard="222"><name>王五</name><location>铁岭</location><grade>99</grade><性别/></student></exam>
|
XMLwork.java | |
package com.java.home4;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
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.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class XMLwork {
public static void main(String[] args) throws Exception {
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder=dbf.newDocumentBuilder();
Document document=documentBuilder.parse("exam.xml");
//1. 向该xml文档里添加一个用户,姓名:王五,;location:铁岭;grade:99
Element newStudent=document.createElement("student");
newStudent.setAttribute("idcard", "222");
newStudent.setAttribute("examid", "333");
Element newStudentName=document.createElement("name");
newStudentName.setTextContent("王五");
Element newStudentLocation=document.createElement("location");
newStudentLocation.setTextContent("铁岭");
Element newStudentGrade=document.createElement("grade");
newStudentGrade.setTextContent("99");
newStudent.appendChild(newStudentName);
newStudent.appendChild(newStudentLocation);
newStudent.appendChild(newStudentGrade);
NodeList rootElement = document.getElementsByTagName("exam");
Node item = rootElement.item(0);
item.appendChild(newStudent);
|
//2. 给每个学生增加一条信息,性别。
NodeList studentList = document.getElementsByTagName("student");
System.out.println(studentList.item(0).getFirstChild().getTextContent());
for(int i=0;i<studentList.getLength();++i){
Element newSex=document.createElement("性别");
//有几个student就要新建几个
studentList.item(i).appendChild(newSex);
}
//3. 删除第一个学生张三的所有信息。
Node rootNode =document.getElementsByTagName("exam").item(0);
rootNode.removeChild(studentList.item(0));
//4. 将第二个学生李四的姓名改为 李四光
for(int i=0;i<studentList.getLength();++i){
for(int j=0;j<studentList.item(i).getChildNodes().getLength();j++){
if( studentList.item(i).getChildNodes().item(j).getNodeName().equals("name") )
if( studentList.item(i).getChildNodes().item(j).getTextContent().equals("李四") )
studentList.item(i).getChildNodes().item(j).setTextContent("李四光");
}
}
//5. 给每个学生增加一个ID属性。
for(int k=0;k<studentList.getLength();++k){
String arg=k+"";
((Element)studentList.item(k)).setAttribute("ID",arg);
}
//xml写入
TransformerFactory tsf =TransformerFactory.newInstance();
Transformer tsTransformer = tsf.newTransformer();
tsTransformer.transform(
new DOMSource(document),
new StreamResult("src/Exam.xml"));
}
}
|