DOM 操作XML(CRUD)
<?xml version="1.0" encoding="UTF-8" standalone="no"?><书架>
<书>
<书名 name="dddd">C语言程序设计</书名>
<作者>张孝祥</作者>
<售价>40</售价>
</书>
<书>
<书名>C++教程</书名>
<作者>自己</作者>
<售价>50</售价>
</书>
</书架>
package com.gbx.it; import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StreamCorruptedException; import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
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.junit.Test;
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;
import org.xml.sax.SAXException; public class XMLDemo {
public String path = "src/book.xml";
/*
* 获得指定的Document
*/
public Document getDocument() throws ParserConfigurationException, SAXException, IOException {
//1: 获得dom解析工厂
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
//2:获得dom解析
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
//获得Document
Document document = documentBuilder.parse(path); return document;
}
/*
* 将XML文件由内存写入硬盘
*/
public void refreshXML(Source xmlSource, Result outputTarget) throws TransformerException {
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer();
transformer.transform(xmlSource, outputTarget);
}
// ----------R------------------
/*
* 遍历DOM树
*/
@Test
public void read1() throws ParserConfigurationException, SAXException, IOException {
Document document = getDocument();
NodeList nodeList = document.getElementsByTagName("书架");
for (int i = 0; i < nodeList.getLength(); ++i) {
listNodes(nodeList.item(i));
}
}
private void listNodes(Node item) {
if (item instanceof Element) {
System.out.println(item.getNodeName());
}
NodeList nodeList = item.getChildNodes();
for (int i = 0; i < nodeList.getLength(); ++i) {
listNodes(nodeList.item(i));
}
}
/*
* 读取标签内的内容 <书名 name="dddd">java web就业</书名>
*/
@Test
public void read2() throws ParserConfigurationException, SAXException, IOException {
Document document = getDocument();
Element book = (Element) document.getElementsByTagName("书名").item(0);
String value = book.getTextContent();
System.out.println("书名: " + value);
}
/*
* 读取标签的属性
*/
@Test
public void read3() throws ParserConfigurationException, SAXException, IOException {
Document document = getDocument();
Element book = (Element) document.getElementsByTagName("书名").item(0);
if (book.hasAttributes()) {
NamedNodeMap nodeMap = book.getAttributes();
for (int i = 0; i < nodeMap.getLength(); ++i) {
String attrName = nodeMap.item(i).getNodeName();
String attrValue = nodeMap.item(i).getNodeValue();
System.out.println("name : " + attrName + " value :" + attrValue);
}
} String value = book.getAttribute("name");
System.out.println(value);
}
// ----------------C------------------- /*
* 添加标签 C
*/
//在指定标签的最后边添加标签
@Test
public void add1() throws ParserConfigurationException, SAXException, IOException, TransformerException {
Document document = getDocument();
Element book = (Element) document.getElementsByTagName("书").item(0);
Element newChild = document.createElement("你行");
newChild.setTextContent("嘿嘿");
book.appendChild(newChild); refreshXML(new DOMSource(document), new StreamResult(new FileOutputStream(path)));
}
//在指定标签的指定位置添加标签
@Test
public void add2() throws ParserConfigurationException, SAXException, IOException, TransformerException {
Document document = getDocument();
Element book = (Element) document.getElementsByTagName("书").item(0); Element newChild = document.createElement("你行");
newChild.setTextContent("嘿嘿"); Element refChild = (Element) document.getElementsByTagName("售价").item(0); book.insertBefore(newChild, refChild); refreshXML(new DOMSource(document), new StreamResult(new FileOutputStream(path)));
}
//添加属性
@Test
public void add3() throws ParserConfigurationException, SAXException, IOException, TransformerException {
Document document = getDocument(); Element e = (Element) document.getElementsByTagName("售价").item(0);
e.setAttribute("value", "RMB");
refreshXML(new DOMSource(document), new StreamResult(new FileOutputStream(path)));
}
//----------------D----------------
//删除标签
@Test
public void delElement() throws ParserConfigurationException, SAXException, IOException, TransformerException {
Document document = getDocument();
Element element = (Element) document.getElementsByTagName("你行").item(0);
element.getParentNode().removeChild(element);
refreshXML(new DOMSource(document), new StreamResult(new FileOutputStream(path)));
}
//删除标签的属性 @Test
public void delAttr() throws ParserConfigurationException, SAXException, IOException, TransformerException {
Document document = getDocument();
Element element = (Element) document.getElementsByTagName("售价").item(0);
element.removeAttribute("value");
refreshXML(new DOMSource(document), new StreamResult(new FileOutputStream(path)));
}
//-------------U------------
@Test
public void update() throws ParserConfigurationException, SAXException, IOException, TransformerException {
Document document = getDocument();
Element element = (Element) document.getElementsByTagName("书名").item(0);
element.setTextContent("C语言程序设计");
refreshXML(new DOMSource(document), new StreamResult(new FileOutputStream(path)));
}
}
参考:方立勋老师视频
DOM 操作XML(CRUD)的更多相关文章
- php中通过DOM操作XML
DOM文档在js里早就接触过,知道DOM不但可以操作html文档,还可以操作XHTML,XML等文档,有着极强的通用性,下面我们通过两个小例子,看看在PHP中是如何用DOM操作XML文档的,和js中差 ...
- php : DOM 操作 XML
DOM 操作 XML 基本用法 XML文件: person.XML <?xml version="1.0" encoding="utf-8" ?> ...
- Java用DOM操作xml
JAXP DOM方式解析XML文档实例增删改查package jiexi; import javax.xml.parsers.DocumentBuilder; import javax.xml.par ...
- java使用DOM操作XML
XML DOM简介 XML DOM 是用于获取.更改.添加或删除 XML 元素的标准. XML 文档中的每个成分都是一个节点. DOM 是这样规定的: 整个文档是一个文档节点 每个 XML 标签是一个 ...
- DOM操作XML文件
一.IE中的XML(IE低版本才支持) 在统一的正式规范出来以前,浏览器对于 XML 的解决方案各不相同.DOM2 级提出了动态创建 XML DOM 规范,DOM3 进一步增强了 XML DOM. 所 ...
- PHP通过DOM操作XML
PHP XML操作类DOMDocument属性及方法 注意大小写一定不能弄错. 属性: Attributes 存储节点的属性列表(只读) childNodes 存储节点的子节点列表(只读) dataT ...
- java DOM 操作xml
1 代码如下: package dom.pasing; import java.io.IOException; import java.io.StringWriter; import javax.xm ...
- ajax——dom对xml和html的操作
上篇文章说到了dom的基础,dom能够操作xml和html,这次主要写利用dom的api去如何去操作xml和html文档. dom操作xml dom操作xml文档之前必须把xml文档装载到xml do ...
- DOM【介绍、HTML中的DOM、XML中的DOM】
什么是DOM? DOM(Document Object Model)文档对象模型,是语言和平台的中立接口. 允许程序和脚本动态地访问和更新文档的内容. 为什么要使用DOM? Dom技术使得用户页面可以 ...
随机推荐
- zabbix用自带模板监控mysql
本身zabbix-agent没有提供对mysql监控的key,所以需要自定义key来应用这个模板 默认的模板有以下三类 mysql.status[var] mysql.ping mysql.versi ...
- Makefile,如何传递宏定义DEBUG【转】
转自:http://blog.csdn.net/linuxheik/article/details/8051598 版权声明:本文为博主原创文章,未经博主允许不得转载. Makefile,如何传递宏定 ...
- Overview of Flashback Technology
Oracle Flashback Query : SELECT AS OFOracle Flashback Version Query :DBMS_FLASHBACK PackageOracle Fl ...
- Codeforces 741A:Arpa's loud Owf and Mehrdad's evil plan(LCM+思维)
http://codeforces.com/problemset/problem/741/A 题意:有N个人,第 i 个人有一个 a[i],意味着第 i 个人可以打电话给第 a[i] 个人,所以如果第 ...
- C# 插件
1.EsFrameWork框架 http://www.oraycn.com/ESFramework_download.aspx
- c# 服务端
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- c#之线程
//Process[] pro= Process.GetProcesses(); //foreach (var item in pro) //{ // Console.WriteLine(item); ...
- Android开发中完全退出程序的三种方法
参考: http://android.tgbus.com/Android/tutorial/201108/363511.shtml Android程序有很多Activity,比如说主窗口A,调用了子窗 ...
- 哈希-Snowflake Snow Snowflakes 分类: POJ 哈希 2015-08-06 20:53 2人阅读 评论(0) 收藏
Snowflake Snow Snowflakes Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 34762 Accepted: ...
- 滑雪 分类: POJ 2015-07-23 19:48 9人阅读 评论(0) 收藏
滑雪 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 83276 Accepted: 31159 Description Mich ...