class StudentServer { public static Document getDocument(String fullFilePath) throws SAXException, IOException, ParserConfigurationException{ DocumentBuilderFactory documentBuilderFactory=DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder=documentBuilderFactory.newDocumentBuilder(); return documentBuilder.parse(fullFilePath); }
DocumentBuilderFactory documentBuilderFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder=documentBuilderFactory.newDocumentBuilder();
documentBuilder.parse(fullFilePath)
//更新xml文档
package com.XML.net; import java.awt.datatransfer.StringSelection; import java.io.IOException; import java.util.Scanner; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; public class XML { public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException { // TODO 自动生成的方法存根 StudentServer studentServer = new StudentServer(); System.out.println("查看所有学生的成绩------view"); System.out.println("按照学生id号码查询学生成绩-----view of"); System.out.println("添加一个学生-----add"); System.out.println("按照id更改一个学生的信息-----change"); System.out.println("按照id去删除一个学生------remove"); System.out.println("保存学生信息-----save"); System.out.println("退出系统--------exit"); String string = new Scanner(System.in).next(); if (string.equals("view")) { System.out.println("所有学生成绩如下:"); StudentServer.showStudent(); } } } class StudentServer { public static Document getDocument(String fullFilePath) throws SAXException, IOException, ParserConfigurationException{ DocumentBuilderFactory documentBuilderFactory=DocumentBuilderFactory.newInstance(); DocumentBuilder documentBuilder=documentBuilderFactory.newDocumentBuilder(); return documentBuilder.parse(fullFilePath); } public static void showStudent() throws ParserConfigurationException, SAXException, IOException { NodeList nodelist=getDocument("src/com/XML/net/Myclasses.xml").getElementsByTagName("student"); for(int i = 0;i<nodelist.getLength();i++) { Element element = (Element) nodelist.item(i); System.out.println("编号\t"+element.getAttribute("sid")+ "名字\t"+getFirstElement(element,"name")+ "java\t"+getFirstElement(element, "java")+ "oracle\t"+getFirstElement(element, "oracle")+ "vb\t"+getFirstElement(element, "vb")); } } public static String getFirstElement(Element element, String name) { return element.getElementsByTagName(name).item(0).getTextContent()+"\t"; } } class Student { private String sid; private String name; private int my_java; private int my_oracle; private int my_vb; public String getSid() { return sid; } public void setSid(String sid) { this.sid = sid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getMy_java() { return my_java; } public void setMy_java(int my_java) { this.my_java = my_java; } public int getMy_oracle() { return my_oracle; } public void setMy_oracle(int my_oracle) { this.my_oracle = my_oracle; } public int getMy_vb() { return my_vb; } public void setMy_vb(int my_vb) { this.my_vb = my_vb; } }
下为存储数据的XML文档
<?xml version="1.0" encoding="UTF-8"?> <students> <student sid="001"> <name>小明</name> <course> <java>90</java> <oracle>90</oracle> <vb>89</vb> </course> </student> <student sid="002"> <name>小李</name> <course> <java>9</java> <oracle>70</oracle> <vb>8</vb> </course> </student> <student sid="003"> <name>小韩</name> <course> <java>90</java> <oracle>70</oracle> <vb>85</vb> </course> </student> <student sid="004"> <name>小明名</name> <course> <java>34</java> <oracle>50</oracle> <vb>58</vb> </course> </student> <student sid="005"> <name>小红</name> <course> <java>29</java> <oracle>39</oracle> <vb>88</vb> </course> </student> </students>