XML解析之使用DOM分析操作XML文档

package forXML;


import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
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.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 * xml解析,你们喜欢的实验报告哟~~~哈哈哈
 * 
 * @author catfish
 *
 */
public class XMLParser {

	//遍历xml文件
	public static void travelXML(Document doc) {
		NodeList classList = doc.getElementsByTagName("班级");
		
		for(int i=0;i<classList.getLength();i++){
			Element clas = (Element) classList.item(i);
			System.out.println("代号:"+clas.getAttribute("代号"));
		}
		
		NodeList stuList = doc.getElementsByTagName("学生");
		for(int i = 0;i<stuList.getLength();i++){
			Element stu = (Element) stuList.item(i);
			System.out.println("name:"+stu.getAttribute("name"));
			NodeList scoNode = stu.getElementsByTagName("各科成绩");
			Element sco = (Element) scoNode.item(0);
			NodeList couList = sco.getElementsByTagName("Course");
			NodeList scoList = sco.getElementsByTagName("Score");
			for(int j = 0;j<couList.getLength() && j<scoList.getLength();j++){
				System.out.print(couList.item(j).getTextContent()+":");
				System.out.println(scoList.item(j).getTextContent());
			}
			
		}
	}
	
	//查找某个学生(宋江)的各科成绩
	public static void query(Document doc){
		NodeList list = doc.getElementsByTagName("学生");
		for(int i=0;i<list.getLength();i++){
			Element stu = (Element) list.item(i);
			if(stu.getAttribute("name").equals("宋江")){
				NodeList couList = stu.getElementsByTagName("Course");
				NodeList scoList = stu.getElementsByTagName("Score");
				for(int j=0;j<couList.getLength() && j<scoList.getLength();j++){
					System.out.print(couList.item(j).getTextContent());
					System.out.println(scoList.item(j).getTextContent());
				}
			}
		}
	}
	
	//添加一个学生节点(武松)的各科成绩
	public static void add(Document doc) throws TransformerException{
		//创建学生节点
		Element newStu = doc.createElement("学生");
		//添加属性
		newStu.setAttribute("name", "武松");
		
		Element newStu_sco = doc.createElement("各科成绩");
		
		
		Element newStu_cou1 = doc.createElement("Course");
		newStu_cou1.setTextContent("计算机基础");
		Element newStu_sco1 = doc.createElement("Score");
		newStu_sco1.setTextContent("99");
		
		Element newStu_cou2 = doc.createElement("Course");
		newStu_cou2.setTextContent("C语言");
		Element newStu_sco2 = doc.createElement("Score");
		newStu_sco2.setTextContent("90");
		
		Element newStu_cou3 = doc.createElement("Course");
		newStu_cou3.setTextContent("计算机网络");
		Element newStu_sco3 = doc.createElement("Score");
		newStu_sco3.setTextContent("95");
		
		Element newStu_cou4 = doc.createElement("Course");
		newStu_cou4.setTextContent("XML技术");
		Element newStu_sco4 = doc.createElement("Score");
		newStu_sco4.setTextContent("100");
		
		newStu_sco.appendChild(newStu_cou1);
		newStu_sco.appendChild(newStu_sco1);
		newStu_sco.appendChild(newStu_cou2);
		newStu_sco.appendChild(newStu_sco2);
		newStu_sco.appendChild(newStu_cou3);
		newStu_sco.appendChild(newStu_sco3);
		newStu_sco.appendChild(newStu_cou4);
		newStu_sco.appendChild(newStu_sco4);
		
		//把各科成绩节点挂在”武松“节点下面
		newStu.appendChild(newStu_sco);
		
		//把”武松“节点挂在doc节点下
		doc.getDocumentElement().appendChild(newStu);
		
		//更新xml文件
		TransformerFactory tFactory = TransformerFactory.newInstance();
		Transformer transformer = tFactory.newTransformer();
		transformer.transform(new DOMSource(doc), new StreamResult("src/forXML/scoreList.xml"));
	}
	
	//删除”宋江“节点
	public static void delete(Document doc) throws TransformerException{
		NodeList list = doc.getElementsByTagName("学生");
		for(int i=0;i<list.getLength();i++){
			Element stu = (Element) list.item(i);
			if(stu.getAttribute("name").equals("宋江")){
//				Node node = stu.getElementsByTagName("各科成绩").item(0);
//				node.getParentNode().removeChild(node);
				stu.getParentNode().removeChild(stu);
			}
		}
		
		//更新xml文件
		TransformerFactory tFactory = TransformerFactory.newInstance();
		Transformer transformer = tFactory.newTransformer();
		transformer.transform(new DOMSource(doc), new StreamResult("src/forXML/scoreList.xml"));
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		try {

			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			Document doc = builder.parse("src/forXML/scoreList.xml");

			travelXML(doc);
			System.out.println("-------------我是一条可爱的分解线--------------");
			query(doc);
			add(doc);
			
			delete(doc);
			System.out.println("-------------我是一条可爱的分解线--------------");
			travelXML(doc);
			
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值