java 修改xml某属性_dom4j修改,获取,增加xml中某个元素的属性值

packagecom.vrv.paw.utils;importjava.io.File;importjava.io.FileNotFoundException;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.UnsupportedEncodingException;importjava.util.Iterator;importjava.util.Map;importorg.dom4j.Attribute;importorg.dom4j.Document;importorg.dom4j.DocumentException;importorg.dom4j.DocumentHelper;importorg.dom4j.Element;importorg.dom4j.io.OutputFormat;importorg.dom4j.io.SAXReader;importorg.dom4j.io.XMLWriter;/*** 操作XML文件的工具类

*

*@authorglw*/

public classXMLUtil {/*** 得到XML文档

*

*@paramxmlFile

* 文件名(路径)

*@returnXML文档对象

*@throwsDocumentException*/

public staticDocument getDocument(String xmlFile) {

SAXReader reader= newSAXReader();

reader.setEncoding("UTF-8");

File file= newFile(xmlFile);try{if (!file.exists()) {return null;

}else{returnreader.read(file);

}

}catch(DocumentException e) {throw new RuntimeException(e + "->指定文件【" + xmlFile + "】读取错误");

}

}/*** 得到XML文档(编码格式-gb2312)

*

*@paramxmlFile

* 文件名(路径)

*@returnXML文档对象

*@throwsDocumentException*/

public staticDocument getDocument_gb2312(String xmlFile) {

SAXReader reader= newSAXReader();

reader.setEncoding("gb2312");

File file= newFile(xmlFile);try{if (!file.exists()) {return null;

}else{returnreader.read(file);

}

}catch(DocumentException e) {throw new RuntimeException(e + "->指定文件【" + xmlFile + "】读取错误");

}

}public staticString getText(Element element) {try{returnelement.getTextTrim();

}catch(Exception e) {throw new RuntimeException(e + "->指定【" + element.getName() + "】节点读取错误");

}

}/*** 增加xml文件节点

*

*@paramdocument

* xml文档

*@paramelementName

* 要增加的元素名称

*@paramattributeNames

* 要增加的元素属性

*@paramattributeValues

* 要增加的元素属性值*/

public static Document addElementByName(Document document, String elementName, Mapattrs, String cdata) {try{

Element root=document.getRootElement();

Element subElement=root.addElement(elementName);for (Map.Entryattr : attrs.entrySet()) {

subElement.addAttribute(attr.getKey(), attr.getValue());

}

subElement.addCDATA(cdata);

}catch(Exception e) {throw new RuntimeException(e + "->指定的【" + elementName + "】节点增加出现错误");

}returndocument;

}/*** 删除xml文件节点*/@SuppressWarnings("unchecked")public staticDocument deleteElementByName(Document document, String elementName) {

Element root=document.getRootElement();

Iterator iterator = root.elementIterator("file");while(iterator.hasNext()) {

Element element=(Element) iterator.next();//根据属性名获取属性值

Attribute attribute = element.attribute("name");if(attribute.getValue().equals(elementName)) {

root.remove(element);

document.setRootElement(root);break;

}

}returndocument;

}/*** 输出xml文件

*

*@paramdocument

*@paramfilePath

*@throwsIOException*/

public static void writeXml(Document document, String filePath) throwsIOException {

File xmlFile= newFile(filePath);

XMLWriter writer= null;try{if(xmlFile.exists())

xmlFile.delete();

writer= new XMLWriter(newFileOutputStream(xmlFile), OutputFormat.createPrettyPrint());

writer.write(document);

writer.close();

}catch(UnsupportedEncodingException e) {

e.printStackTrace();

}catch(FileNotFoundException e) {

e.printStackTrace();

}catch(IOException e) {

e.printStackTrace();

}finally{if (writer != null)

writer.close();

}

}/*** 创建Document及根节点

*

*@paramrootName

*@paramattributeName

*@paramattributeVaule

*@return

*/

public staticDocument createDocument(String rootName, String attributeName, String attributeVaule) {

Document document= null;try{

document=DocumentHelper.createDocument();

Element root=document.addElement(rootName);

root.addAttribute(attributeName, attributeVaule);

}catch(Exception e) {throw new RuntimeException(e + "->创建的【" + rootName + "】根节点出现错误");

}returndocument;

}/*** 删除xml文件节点*/@SuppressWarnings("unchecked")public staticDocument deleteElementAddressByName(Document document, String elementName) {

Element root=document.getRootElement();

Iterator iterator = root.elementIterator("address");while(iterator.hasNext()) {

Element element=(Element) iterator.next();//根据属性名获取属性值

Attribute attribute = element.attribute("name");if(attribute.getValue().equals(elementName)) {

root.remove(element);

document.setRootElement(root);break;

}

}returndocument;

}/*** 删除属性等于某个值的元素

*@paramdocument XML文档

*@paramxpath xpath路径表达式

*@paramattrName 属性名

*@paramattrValue 属性值

*@return

*/@SuppressWarnings("unchecked")public staticDocument deleteElementByAttribute(Document document, String xpath, String attrName, String attrValue) {

Iterator iterator =document.selectNodes(xpath).iterator();while(iterator.hasNext()) {

Element element=(Element) iterator.next();

Element parentElement=element.getParent();//根据属性名获取属性值

Attribute attribute =element.attribute(attrName);if(attribute.getValue().equals(attrValue)) {

parentElement.remove(element);

}

}returndocument;

}

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值