xmlファイルを小さいデータベースとして、学生の添削調査の簡単な例を行います。
1.xmlファイル:
<?xml version="1.0" encoding="UTF-8"?><Students>
<student id="2">
<name>ttt</name>
<age>44</age>
</student>
<student id="3">
<name>linda2</name>
<age>22</age>
</student>
<student id="4">
<name>linda3</name>
<age>23</age>
</student>
<student id="5">
<name>jack</name>
<age>2</age>
</student>
<student id="1">
<name>yyh1</name>
<age>22</age>
</student>
</Students>
2.Javaコード
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
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.NodeList;
import org.w3c.dom.Text;
import org.xml.sax.SAXException;
// , ,
public class StudentManager {
public static void main(String[] args) {
try {
Document doc = Domutils.getDoc(new File("xml "));
Scanner input = new Scanner(System.in);
System.out.println("
:
1.
2.
3.
( )");
int num = input.nextInt();
if(num == 1) {
addStudent(doc);
}else if(num == 2) {
delStudent(doc);
}else if(num == 3) {
updStudent(doc);
}
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
}
//
private static void updStudent(Document doc) {
Element updStudent = null;
Scanner input = new Scanner(System.in);
System.out.println(" :");
String studentid = input.nextLine();
System.out.println(" :");
String newName = input.nextLine();
System.out.println(" :");
String newAge = input.nextLine();
// ,for
NodeList list = doc.getElementsByTagName("student");
for(int i = 0; i <list.getLength(); i++) {
if(studentid.equals(list.item(i).getAttributes().getNamedItem("id").getNodeValue())){
updStudent = (Element) doc.getElementsByTagName("student").item(i).getFirstChild().getParentNode();
// name
updStudent.getElementsByTagName("name").item(i).getFirstChild().setNodeValue(newName);
// age
updStudent.getElementsByTagName("age").item(i).getFirstChild().setNodeValue(newAge);
}else{
break;
}
}
// ,
Element root = doc.getDocumentElement();
transform(root);
System.out.println(updStudent);
}
//
private static void delStudent(Document doc) {
Scanner input = new Scanner(System.in);
//
System.out.println(" :");
String studentid = input.nextLine();
Element root = doc.getDocumentElement();
// , ,
NodeList list = doc.getElementsByTagName("student");
for(int i = 0; i < list.getLength(); i++) {
if((studentid).equals(list.item(i).getAttributes().getNamedItem("id").getNodeValue())){
Element delStudent = (Element) doc.getElementsByTagName("student").item(i).getFirstChild().getParentNode();
root.removeChild(delStudent);
break;
}else {
System.out.println(" ");
break;
}
}
//
transform(root);
}
//
private static void addStudent(Document doc) {
// System.out.println(doc.getElementsByTagName("student").item(1).getAttributes().getNamedItem("id").getNodeValue());
Element root = doc.getDocumentElement();
//
Scanner input = new Scanner(System.in);
System.out.println(" :id = ");
// , , ,
NodeList list = doc.getElementsByTagName("student");
String studentid = input.nextLine();
for(int i = 0; i < list.getLength(); i++) {
if(studentid.equals(list.item(i).getAttributes().getNamedItem("id").getNodeValue())){
System.out.println(" , :");
studentid = input.nextLine();
}else {
break;
}
}
System.out.println(" :name = ");
String name_value = input.nextLine();
System.out.println(" :age = ");
String age_value = input.nextLine();
//
Element student = doc.createElement("student");
Element name = doc.createElement("name");
Element age = doc.createElement("age");
Text namText = doc.createTextNode(name_value);
Text ageText = doc.createTextNode(age_value);
//
root.appendChild(student);
student.appendChild(name);
student.appendChild(age);
student.setAttribute("id", studentid);
name.appendChild(namText);
age.appendChild(ageText);
//
transform(root);
}
//
private static void transform(Element root)
throws TransformerFactoryConfigurationError {
TransformerFactory factory = TransformerFactory.newInstance();
try {
Transformer tf = factory.newTransformer();
tf.transform(new DOMSource(root), new StreamResult(new File("src/com/briup/dom/student.xml")));
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
}
}
2.Dom解析ファイル(解析ファイルを取得した部分をパッケージ化)
import java.io.File;
import java.io.IOException;
import java.nio.file.attribute.AclEntry.Builder;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
public class Domutils {
public static Document getDoc(File file) throws SAXException, IOException, ParserConfigurationException {
//
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
// builder
DocumentBuilder builder = factory.newDocumentBuilder();
// ,
Document document = builder.parse(file);
return document;
}
}
以上のxmlファイルを小さなデータベースとして、学生の添削調査を行う簡単な例は小編集が皆さんに共有した内容の全部です。