用JAXP对xml文档进行DOM编程


class StudentServer {
	public static Document getDocument(String fullFilePath) throws SAXException, IOException, ParserConfigurationException{
		DocumentBuilderFactory documentBuilderFactory=DocumentBuilderFactory.newInstance();
		DocumentBuilder documentBuilder=documentBuilderFactory.newDocumentBuilder();
		return documentBuilder.parse(fullFilePath);
		
	}



1.得到创建DOM解析器的工厂
DocumentBuilderFactory documentBuilderFactory=DocumentBuilderFactory.newInstance();
		



2.利用工厂得到一个DOM解析器对象
DocumentBuilder documentBuilder=documentBuilderFactory.newDocumentBuilder();



3.调用DOM解析器对象的Pase方法对文档进行解析,得到代表XML文档的document
documentBuilder.parse(fullFilePath)





利用Document对文档进行CRUD


1.读取文档指定标签的内容
Element element = Document.getElementsByTagName("指定的标签名");
String content = element.getTextContent()


2.读取标签属性的值
Element element = Document.getElementsByTagName("指定的标签名");
String value = element.getAttribute("属性名");


3.在xml文档中添加内容
3.1 用标签封装数据,添加到xml文档中
Element e = document.createElement("标签名");  //创建标签
e.setTextContent("指定的数据");

parentNode.appendChild(e);  //把需要添加的节点挂到父节点上

//更新xml文档
TransformerFactory factory = TransformerFactory.newInstance();
Transformer tf = factory.transform();
tf.transform(new DomSource(document),new StreamResult(new File("xxx.xml")));

3.2 用标签的属性封装数据,并添加到xml上
Element e = document.createElement("标签名");  //创建标签
e.setAttribute("name","value");


//更新xml文档


4、删除xml文档中的数据,由于数据会以标签的内容或标签的属性的形式存放,所以删除数据就是删除标签和属性
4.1 删除标签
Element e = document.getElementsByTagName("要删除的标签名").items[0];
e.getParentNode.removeChild(e);
4.2 删除标签的属性
Element e = document.getElementsByTagName("要删除属性的标签名").items[0];
e.removeAttribute("指定要删除的属性名");


5.修改xml文档中的数据,修改标签体中封装的数据、或修改属性值
5.1 修改标签体中封装的数据
Element e = document.getElementsByTagName("指定要修改数据的标签名").items[0];
e.setTextContent("新的数据");
//更新xml文档
5.2 修改属性值
Element e = document.getElementsByTagName("指定要修改属性的标签名").items[0];
e.setAttribute("属性名","新的属性值");

//更新xml文档






利用XML文档实现学生成绩查询系统(显示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>



你可能感兴趣的:(xml,dom,文档)