java对xml 增删改_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

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

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 selectXML(String filePath){

List colorValueList = new ArrayList();

try{

//读取传入的路径,返回一个document对象

Document document = loadInit(filePath);

//获取叶节点

NodeList nodeList = document.getElementsByTagName("color");

//遍历叶节点

for(int i=0; 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;

}

}

7007b384-fab3-4779-9171-229d0664b6b5

black

2222

22222

421f481e-790c-41be-91e3-27d215b73ce2

colorValue

100

200

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值