第6章 課後作業
第1題 編寫XML文件
<?xml version="1.0" encoding="UTF-8"?>
<sorces>
<student id="1">
<name>張三</name>
<course>JAVA</course>
<score>100分</score>
</student>
<student id="2">
<name>李四</name>
<course>JAVA</course>
<score>90分</score>
</student>
</sorces>
第2題 使用DOM解析第1題編寫的XML文件
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class Show {
public static void main(String[] args) {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc =db.parse("Scores.xml");
NodeList list = doc.getElementsByTagName("student");
for (int i = 0; i < list.getLength(); i++) {
Node stu = list.item(i);
Element element = (Element) stu;
String str = element.getAttribute("id");
System.out.println("編號:"+str);
NodeList listName = element.getElementsByTagName("name");
NodeList listCourse = element.getElementsByTagName("course");
NodeList listScore = element.getElementsByTagName("score");
for (int j = 0; j < listScore.getLength(); j++) {
Element childName = (Element) listName.item(j);
Element childCourse = (Element) listCourse.item(j);
Element childScore = (Element) listScore.item(j);
String name = childName.getFirstChild().getNodeValue();
String course = childCourse.getFirstChild().getNodeValue();
String score = childScore.getFirstChild().getNodeValue();
System.out.println("姓名:"+name+"\n課程:"+course+"\n分數:"+score+"\n");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
第3題 使用DOM對XML文件進行刪,改,增
import java.io.FileOutputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
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.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class DelChangeAdd {
Document doc = null;
public static void main(String[] args) {
DelChangeAdd dca = new DelChangeAdd();
dca.load();
dca.delete();
dca.change();
dca.save();
dca.add();
dca.save();
}
public void load() {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
doc = db.parse("Scores.xml");
} catch (Exception e) {
e.printStackTrace();
}
}
public void delete() {
NodeList list = doc.getElementsByTagName("student");
for (int i = 0; i < list.getLength(); i++) {
Node stu = list.item(i);
Element element = (Element) stu;
String str = element.getAttribute("id");
if (str.equals("1")) {
element.getParentNode().removeChild(element);
}
}
}
public void save() {
TransformerFactory tff = TransformerFactory.newInstance();
try {
Transformer tf = tff.newTransformer();
DOMSource doms = new DOMSource(doc);
tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
StreamResult result = new StreamResult(new FileOutputStream("Scores1.xml"));
tf.transform(doms, result);
} catch (Exception e) {
e.printStackTrace();
}
}
public void change() {
NodeList nl = doc.getElementsByTagName("student");
for (int i = 0; i < nl.getLength(); i++) {
Node stu = nl.item(i);
Element element = (Element) stu;
String str = element.getAttribute("id");
if (str.equals("2")) {
NodeList listScore = element.getElementsByTagName("score");
Element getScore = (Element) listScore.item(0);
getScore.setTextContent("60分");
}
}
}
public void add() {
Element name = doc.createElement("name");
name.setTextContent("王五");
Element course = doc.createElement("course");
course.setTextContent("JAVA");
Element score = doc.createElement("score");
score.setTextContent("88分");
Element student = doc.createElement("student");
student.setAttribute("id", "3");
student.appendChild(name);
student.appendChild(course);
student.appendChild(score);
Element sorces = (Element) doc.getElementsByTagName("sorces").item(0);
sorces.appendChild(student);
}
}
第4題 DOM4J解析第1題描述成績資訊的XML,輸出成績資訊
import java.util.Iterator;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class DOM4JScore {
public static void main(String[] args) {
SAXReader reader = new SAXReader();
Document doc = null;
try {
doc = reader.read("Scores.xml");
Element root = doc.getRootElement();
for (Iterator iterator = root.elementIterator(); iterator.hasNext();) {
Element element = (Element) iterator.next();
String score = element.elementText("score");
System.out.println(score);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}