dom增删改查xml文件

 

public class DOMForXml {
		/**
		 * 获得doc对象
		 */
		public Document getDocument(String fileName) {
			Document document = null;
			try {
				DocumentBuilderFactory factory = DocumentBuilderFactory
						.newInstance();
				DocumentBuilder builder = factory.newDocumentBuilder();
				document = builder.parse(new File(fileName));

			} catch (Exception e) {
				e.printStackTrace();
			}

			return document;
		}

		/**
		 * 将改动持久到文件
		 */
		public void modifyFile(Document doc, String distFileName) {
			try {
				TransformerFactory tf = TransformerFactory.newInstance();
				Transformer tfer = tf.newTransformer();
				DOMSource dsource = new DOMSource(doc);
				StreamResult sr = new StreamResult(new File("student.xml"));
				tfer.transform(dsource, sr);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		/**
		 * 解析
		 */
		public void paseXml(String fileName) {
			Document document = getDocument(fileName);
			NodeList nodeList = document.getElementsByTagName("student");
			for (int i = 0; i < nodeList.getLength(); i++) {
				StringBuilder sb = new StringBuilder();
				sb.append("姓名:"
						+ document.getElementsByTagName("name").item(i)
								.getFirstChild().getNodeValue());
				sb.append(" , ");
				sb.append("年龄:"
						+ document.getElementsByTagName("age").item(i)
								.getFirstChild().getNodeValue());
				sb.append(" , ");
				sb.append("性别:"
						+ document.getElementsByTagName("sex").item(i)
								.getFirstChild().getNodeValue());
				sb.append(" , ");
				sb.append("地址:"
						+ document.getElementsByTagName("address").item(i)
								.getFirstChild().getNodeValue());
				System.out.println(sb.toString());
			}
		}

		/**
		 * 创建一个新的学生
		 */
		public void addNewNode(String name, String age, String sex,
				String address) {
			try {
				Document document = getDocument("student.xml");
				NodeList nodeList = document.getElementsByTagName("students");
				// 创建新的节点
				Node studentNode = document.createElement("student");
				Node nameNode = document.createElement("name");
				nameNode.appendChild(document.createTextNode(name));
				Node ageNode = document.createElement("age");
				ageNode.appendChild(document.createTextNode(age));
				Node sexNode = document.createElement("sex");
				sexNode.appendChild(document.createTextNode(sex));
				Node addressNode = document.createElement("address");
				addressNode.appendChild(document.createTextNode(address));
				// 添加节点
				studentNode.appendChild(nameNode);
				studentNode.appendChild(ageNode);
				studentNode.appendChild(sexNode);
				studentNode.appendChild(addressNode);
				nodeList.item(0).appendChild(studentNode);
				// 此时真正的处理将新数据添加到文件中(磁盘)
				TransformerFactory tf = TransformerFactory.newInstance();
				Transformer tfer = tf.newTransformer();
				DOMSource dsource = new DOMSource(document);
				StreamResult sr = new StreamResult(new File("student.xml"));
				tfer.transform(dsource, sr);
			} catch (Exception e) {
				e.printStackTrace();
			}

			paseXml("student.xml");
		}

		/**
		 * 删除一个节点
		 */
		public void deleteNode(String name) {
			Document document = getDocument("student.xml");

			NodeList nodeList = document.getElementsByTagName("name");
			for (int i = 0; i < nodeList.getLength(); i++) {
				String value = nodeList.item(i).getFirstChild()
						.getTextContent();
				if (name != null && name.equalsIgnoreCase(value)) {
					Node parentNode = nodeList.item(i).getParentNode();
					document.getFirstChild().removeChild(parentNode);
				}
			}
			modifyFile(document, "student.xml");
		}

		/**
		 * 根据name修改某个节点的内容
		 */
		public void updateNode(String name) {
			Document document = getDocument("student.xml");
			NodeList nodeList = document.getElementsByTagName("name");
			for (int i = 0; i < nodeList.getLength(); i++) {
				String value = nodeList.item(i).getFirstChild()
						.getTextContent();
				if (name != null && name.equalsIgnoreCase(value)) {
					Node parentNode = nodeList.item(i).getParentNode();
					NodeList nl = parentNode.getChildNodes();
					for (int j = 0; j < nl.getLength(); j++) {
						String modifyNode = nl.item(j).getNodeName();
						if (modifyNode.equalsIgnoreCase("age")) {
							nl.item(j).getFirstChild().setTextContent("25");
						}
					}
				}
			}
			modifyFile(document, "student.xml");
		}
	}
 
<?xml version="1.0" encoding="UTF-8" ?>  
 <students>  
    <student>  
        <name>TigerLee</name>  
        <age>26</age>  
        <sex>男</sex>  
        <address>北京海淀</address>  
    </student>  
    <student>  
        <name>Snow</name>  
        <age>24</age>  
        <sex>女</sex>  
        <address>山东</address>  
    </student>  
</students>
 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值