java对xml文件做增删改查------摘录
package com.wss;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Transformer;
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.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
public class GPS_GNSS_XML_Color {
//删除节点时传入的参数
private static String deleteNumber;
//修改节点时传入的参数
private static String updateNumber;
//读取传入的路径,返回一个document对象
public static Document loadInit(String filePath){
Document document = null;
try{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.parse(new File(filePath));
document.normalize();
return document;
}catch(Exception e){
e.printStackTrace();
System.out.println(e.getMessage());
return null;
}
}
/**
* 删除制定的xml
* @param filePath
* @return
*/
public static boolean deleteXML(String filePath){
deleteNumber = "421f481e-790c-41be-91e3-27d215b73ce2";
Document document = loadInit(filePath);
try{
NodeList nodeList = document.getElementsByTagName("color");
for(int i=0; i<nodeList.getLength(); i++){
String number_ = document.getElementsByTagName("number").item(i).getFirstChild().getNodeValue();
//删除节点时传入的参数
if(number_.equals(deleteNumber)){
Node node = nodeList.item(i);
node.getParentNode().removeChild(node);
saveXML(document, filePath);
}
}
return true;
}catch(Exception e){
e.printStackTrace();
System.out.println(e.getMessage());
return false;
}
}
/**
* 修改制定的xml
* @param filePath
* @return
*/
public static boolean updateXML(String filePath){
updateNumber = "421f481e-790c-41be-91e3-27d215b73ce2";
//读取传入的路径,返回一个document对象
Document document = loadInit(filePath);
try{
//获取叶节点
NodeList nodeList = document.getElementsByTagName("color");
//遍历叶节点
for(int i=0; i<nodeList.getLength(); i++){
String number = document.getElementsByTagName("number").item(i).getFirstChild().getNodeValue();
String colorValue = document.getElementsByTagName("colorValue").item(i).getFirstChild().getNodeValue();
Double minValue = Double.parseDouble(document.getElementsByTagName("minValue").item(i).getFirstChild().getNodeValue());
Double maxValue = Double.parseDouble(document.getElementsByTagName("maxValue").item(i).getFirstChild().getNodeValue());
//修改节点时传入的参数
if(number.equals(updateNumber)){
document.getElementsByTagName("colorValue").item(i).getFirstChild().setNodeValue("black");
document.getElementsByTagName("minValue").item(i).getFirstChild().setNodeValue("2222");
document.getElementsByTagName("maxValue").item(i).getFirstChild().setNodeValue("22222");
System.out.println();
}
}
saveXML(document, filePath);
return true;
}catch(Exception e){
e.printStackTrace();
System.out.println(e.getMessage());
return false;
}
}
/**
* 添加节点
* @param filePath
* @return
*/
public static boolean addXML(String filePath){
try{
//读取传入的路径,返回一个document对象
Document document = loadInit(filePath);
//创建叶节点
Element eltColor = document.createElement("color");
Element eltNumber = document.createElement("number");//创建叶节点的第一个元素
Element eltColorValue = document.createElement("colorValue");//创建叶节点的第二个元素
Element eltMinValue = document.createElement("minValue");//创建叶节点的第三个元素
Element eltMaxValue = document.createElement("maxValue");//创建叶节点的第四个元素
Text number_ = document.createTextNode(UUID.randomUUID().toString());//创建叶节点的第一个元素下的文本节点
eltNumber.appendChild(number_);//把该文本节点加入到叶节点的第一个元素里面
Text colorValue_ = document.createTextNode("colorValue");//创建叶节点的第二个元素下的文本节点
eltColorValue.appendChild(colorValue_);//把该文本节点加入到叶节点的第二个元素里面
Text minValue_ = document.createTextNode("100");//创建叶节点的第三个元素下的文本节点
eltMinValue.appendChild(minValue_);//把该文本节点加入到叶节点的第三个元素里面
Text maxValue_ = document.createTextNode("200");//创建叶节点的第四个元素下的文本节点
eltMaxValue.appendChild(maxValue_);//把该文本节点加入到叶节点的第四个元素里面
//把叶节点下的元素加入到叶节点下
eltColor.appendChild(eltNumber);
eltColor.appendChild(eltColorValue);
eltColor.appendChild(eltMinValue);
eltColor.appendChild(eltMaxValue);
//获取根节点
Element eltRoot = document.getDocumentElement();
//把叶节点加入到根节点下
eltRoot.appendChild(eltColor);
//更新修改后的源文件
saveXML(document, filePath);
return true;
}catch(Exception e){
e.printStackTrace();
System.out.println(e.getMessage());
return false;
}
}
/**
* 把修改后的document写进源文件(更新源文件)
* @param document
* @param filePath
* @return
*/
public static boolean saveXML(Document document, String filePath){
try{
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(new File(filePath));
transformer.transform(source, result);
return true;
}catch(Exception e){
e.printStackTrace();
System.out.println(e.getMessage());
return false;
}
}
/**
* 获取xml文件的所有记录
* @param filePath
* @return
*/
public static List<ColorValue> selectXML(String filePath){
List<ColorValue> colorValueList = new ArrayList<ColorValue>();
try{
//读取传入的路径,返回一个document对象
Document document = loadInit(filePath);
//获取叶节点
NodeList nodeList = document.getElementsByTagName("color");
//遍历叶节点
for(int i=0; i<nodeList.getLength(); i++){
ColorValue colorValue = new ColorValue();
String number_ = document.getElementsByTagName("number").item(i).getFirstChild().getNodeValue();
String colorValue_ = document.getElementsByTagName("colorValue").item(i).getFirstChild().getNodeValue();
Double minValue_ = Double.parseDouble(document.getElementsByTagName("minValue").item(i).getFirstChild().getNodeValue());
Double maxValue_ = Double.parseDouble(document.getElementsByTagName("maxValue").item(i).getFirstChild().getNodeValue());
colorValue.setNumber(number_);
colorValue.setColorValue(colorValue_);
colorValue.setMinValue(minValue_);
colorValue.setMaxValue(maxValue_);
colorValueList.add(colorValue);
}
return colorValueList;
}catch(Exception e){
e.printStackTrace();
System.out.println(e.getMessage());
return null;
}
}
}
package com.wss;
public class ColorValue {
private String number;
private String colorValue;
private Double minValue;
private Double maxValue;
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getColorValue() {
return colorValue;
}
public void setColorValue(String colorValue) {
this.colorValue = colorValue;
}
public Double getMinValue() {
return minValue;
}
public void setMinValue(Double minValue) {
this.minValue = minValue;
}
public Double getMaxValue() {
return maxValue;
}
public void setMaxValue(Double maxValue) {
this.maxValue = maxValue;
}
}
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Colors>
<color>
<number>7007b384-fab3-4779-9171-229d0664b6b5</number>
<colorValue>black</colorValue>
<minValue>2222</minValue>
<maxValue>22222</maxValue>
</color>
<color>
<number>421f481e-790c-41be-91e3-27d215b73ce2</number>
<colorValue>colorValue</colorValue>
<minValue>100</minValue>
<maxValue>200</maxValue>
</color>
</Colors>
java对xml文件做增删改查------摘录的更多相关文章
- java对xml文件做增删改查
http://www.cnblogs.com/wangchenyang/archive/2011/08/23/2150530.html http://www.blogjava.net/weishuan ...
- Java使用DOM4J对XML文件进行增删改查操作
Java进行XML文件操作,代码如下: package com.founder.mrp.util; import java.io.File; import java.util.ArrayList; i ...
- 使用dom4j对xml文件进行增删改查
1.使用dom4j技术对dom_demo.xml进行增删改查 首选要下载dom4j的jar包 在官网上找不到,网上搜索了一下在这个链接:http://sourceforge.net/projects/ ...
- Asp.Net 操作XML文件的增删改查 利用GridView
不废话,直接上如何利用Asp.NET操作XML文件,并对其属性进行修改,刚开始的时候,是打算使用JS来控制生成XML文件的,但是最后却是无法创建文件,读取文件则没有使用了 index.aspx 文件 ...
- php对xml文件的增删改查
源文件<?xml version="1.0" encoding="utf-8"?><root> <endTime>2016 ...
- C# 本地xml文件进行增删改查
项目添加XML文件:FaceXml.xml,并复制到输出目录 FaceXml.xml <?xml version="1.0" encoding="utf-8&quo ...
- xml 文件的增删改查
序列化和反序列化helper using System; using System.Collections.Generic; using System.Linq; using System.Text; ...
- MyBatis学习 之 二、SQL语句映射文件(2)增删改查、参数、缓存
目录(?)[-] 二SQL语句映射文件2增删改查参数缓存 select insert updatedelete sql parameters 基本类型参数 Java实体类型参数 Map参数 多参数的实 ...
- MyBatis学习(二)、SQL语句映射文件(2)增删改查、参数、缓存
二.SQL语句映射文件(2)增删改查.参数.缓存 2.2 select 一个select 元素非常简单.例如: <!-- 查询学生,根据id --> <select id=" ...
随机推荐
- windows 10启动盘制作工具
Rufus 官方网站:http://rufus.akeo.ie/
- leetcode 137
137. Single Number II Given an array of integers, every element appears three times except for one. ...
- 通过WinForm控件创建的WPF控件无法输入的问题
今天把写的一个WPF程序发布到别的机器上执行,发现一个比较奇怪的问题:在那个机器上用英文输入法无法输入数字,非要切换到中文输入法才行:但在我的机器上却是好好的. 最开始以为是输入法的问题,弄了好一阵子 ...
- VC++ 浅谈VS2010中CMFCToolBar的用法
本文将给大家介绍Visual Studio 2010中CMFCToolBar的用法,CMFCToolBar可以让用户自定义工具栏图标,使用静态成员函数SetUserImages()将一个CMFCToo ...
- web应用中对配置文件的包装
<bean id="placeholderConfig" class="com.shz.utils.AdvancedPlaceholderConfigurer&qu ...
- asp.net GridView控件的列属性
BoundField 默认的数据绑定类型,通常用于显示普通文本 CheckBoxField 显示布尔类型的数据.绑定数据为TRUE时,复选框数据绑定列为选中状态:绑定数据为FALSE时,则显示未选中状 ...
- 清除浮动的 why
如果你想第三个p不被前面的浮动层所影响,就对它进行清除如果没有清除,第三个层就会移到第一个p下面 记住!!浮动是用来布局的~你看你的网页设计图,好几个版块在一条线上就是要浮动了,不需要浮动就是版块跟前 ...
- Qrels supervision information以及document collection,如何划分为train、test,保证test中doc对于train来说是new document
简单的思想:转换为最小割问题 无向图的全局最小割算法:Stoer-Wagner算法 简介见:Wiki介绍得比较好并有源代码 最小割算法:http://blog.csdn.net/markpen/art ...
- mysql学习(2)-MySQL服务器优化
调优思路: 1.数据库设计与规划--以后再修该很麻烦,估计数据量,使用什么存储引擎 2.数据的应用--怎样取数据,sql语句的优化 3.mysql服务优化--内存的使用,磁盘的使用 4.操作系统的优化 ...
- 今天想用jquery来实现div的拖放功能
html5标签.拖放(Drag 和 drop)是 HTML5 标准的组成部分. 步骤一:首先设置标签可以被拖 draggable="true" 步骤二:选取被拖的标签,和要放置被拖 ...