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=" ...
随机推荐
- Linux ToolChain (二) --- Linker (1)链接选项 -L -rpath -rpath-link
一.动态库的链接和链接选项-L,-rpath-link,-rpath (1). 现代连接器在处理动态库时将链接时路径(Link-time path)和运行时路径(Run-time path)分开, 用 ...
- HIVE 启动出错总结
1: [centos@centos4- bin]$ hive /home/centos/app/apache-hive--bin/bin/hive: line : /tmp/centos/stderr ...
- python之fabric(二):执行模式(转)
执行模式 执行模式可以让你在多个主机上执行多个任务. 执行策略: 默认fabric是单个有序地执行方法,其行为如下: 1. 创建一系列任务,通过fab任务执行要执行的任务: 2. 根据主机列表定义,去 ...
- Bootstrap修改input file默认样式
html部分 <div class="form-group"> <label class="col-sm-3 control-label"&g ...
- spark1.5.1环境搭建
今天一个小伙伴找我搞spark,搞了一个中午都没搭建好.心里痒痒的.索性自己重来了一遍. 本来很简单的事情,被这小子搞的老复杂了.究其原因,有以下几点: 下载的软件不对,我用的是这两个软件 spark ...
- Queue、进程、线程、协程
参考博客地址 http://www.cnblogs.com/alex3714/articles/5230609.html 1.python GIL全局解释器锁 python调用的操作系统的原生线程,当 ...
- sql基础知识(新手必备)
一.简单查询 1.查询所有数据,查询部分列数据,列别名 SELECT * FROM 表名 SELECT 列1 AS 'BIAOTI1','BIAOTI2'=列2 FROM 表名 2.查询不重复的数据 ...
- isa指针
转载自 http://www.cnblogs.com/zhangdashao/p/4438540.html 可以去这里看详细的. 每个Objective-C对象都有一个隐藏的数据结构,这个数据结构是O ...
- 计算机病毒实践汇总四:netcat使用方法
在尝试学习分析的过程中,判断结论不一定准确,只是一些我自己的思考和探索.敬请批评指正! 1. netcat概述 (1)特性 "TCP/IP协议栈瑞士军刀",可以被用在支持端口扫描. ...
- java文件乱码
要让一个 Java 源文件打开时编码格式为 UTF-8,需要做2件事情:1)设置Java 源文件的默认编码格式为UTF-8:2)设置workspace的编码格式为UTF-8. 相应设置如下: 设置 J ...