dom4j修改,获取,增加xml中某个元素的属性值
来源:互联网 发布:java数组sort方法 编辑:程序博客网 时间:2024/03/28 18:14
xml文件:
<?xml version="1.0" encoding="UTF-8"?><vrvscript> <item ID="1021" isSelf="n"/> <item ID="1023" isSelf="n"/> <item ID="1003" isSelf="n"/> <item ID="1020" isSelf="n"/> <item ID="1022" isSelf="n"/> </vrvscript>
修改属性值:
要把每个item元素的“isSelf”属性值修改为“y”
// 获取XMLDocument document = XMLUtil.getDocument(xmlPath);Element root = document.getRootElement(); Iterator<?> ruleNodes = root.elementIterator("item");while (ruleNodes.hasNext()) { Element ruleElement = (Element) ruleNodes.next(); // 修改isSelf的属性值 Attribute isSelfAttr = ruleElement.attribute("isSelf"); isSelfAttr.setValue("n"); }writeXml(document, xmlPath.getPath());/** * 输出xml文件 * * @param document * @param filePath * @throws IOException */ public static void writeXml(Document document, String filePath) throws IOException { File xmlFile = new File(filePath); XMLWriter writer = null; try { if (xmlFile.exists()) xmlFile.delete(); writer = new XMLWriter(new FileOutputStream(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(); } }
获取属性值:
采用Element类的attributeValue方法
String policyName = root.attributeValue("PolicyName");
给XML元素增加属性
Element ruleElement = root.addElement("item");ruleElement.addAttribute("ID", "1101");ruleElement.addAttribute("isSelf", "y");
提供一个dom4j操作XML的工具类:
package com.vrv.paw.utils;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.UnsupportedEncodingException;import java.util.Iterator;import java.util.Map;import org.dom4j.Attribute;import org.dom4j.Document;import org.dom4j.DocumentException;import org.dom4j.DocumentHelper;import org.dom4j.Element;import org.dom4j.io.OutputFormat;import org.dom4j.io.SAXReader;import org.dom4j.io.XMLWriter;/** * 操作XML文件的工具类 * * @author glw */public class XMLUtil { /** * 得到XML文档 * * @param xmlFile * 文件名(路径) * @return XML文档对象 * @throws DocumentException */ public static Document getDocument(String xmlFile) { SAXReader reader = new SAXReader(); reader.setEncoding("UTF-8"); File file = new File(xmlFile); try { if (!file.exists()) { return null; } else { return reader.read(file); } } catch (DocumentException e) { throw new RuntimeException(e + "->指定文件【" + xmlFile + "】读取错误"); } } /** * 得到XML文档(编码格式-gb2312) * * @param xmlFile * 文件名(路径) * @return XML文档对象 * @throws DocumentException */ public static Document getDocument_gb2312(String xmlFile) { SAXReader reader = new SAXReader(); reader.setEncoding("gb2312"); File file = new File(xmlFile); try { if (!file.exists()) { return null; } else { return reader.read(file); } } catch (DocumentException e) { throw new RuntimeException(e + "->指定文件【" + xmlFile + "】读取错误"); } } public static String getText(Element element) { try { return element.getTextTrim(); } catch (Exception e) { throw new RuntimeException(e + "->指定【" + element.getName() + "】节点读取错误"); } } /** * 增加xml文件节点 * * @param document * xml文档 * @param elementName * 要增加的元素名称 * @param attributeNames * 要增加的元素属性 * @param attributeValues * 要增加的元素属性值 */ public static Document addElementByName(Document document, String elementName, Map<String, String> attrs, String cdata) { try { Element root = document.getRootElement(); Element subElement = root.addElement(elementName); for (Map.Entry<String, String> attr : attrs.entrySet()) { subElement.addAttribute(attr.getKey(), attr.getValue()); } subElement.addCDATA(cdata); } catch (Exception e) { throw new RuntimeException(e + "->指定的【" + elementName + "】节点增加出现错误"); } return document; } /** * 删除xml文件节点 */ @SuppressWarnings("unchecked") public static Document deleteElementByName(Document document, String elementName) { Element root = document.getRootElement(); Iterator<Object> 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; } } return document; } /** * 输出xml文件 * * @param document * @param filePath * @throws IOException */ public static void writeXml(Document document, String filePath) throws IOException { File xmlFile = new File(filePath); XMLWriter writer = null; try { if (xmlFile.exists()) xmlFile.delete(); writer = new XMLWriter(new FileOutputStream(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及根节点 * * @param rootName * @param attributeName * @param attributeVaule * @return */ public static Document 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 + "】根节点出现错误"); } return document; } /** * 删除xml文件节点 */ @SuppressWarnings("unchecked") public static Document deleteElementAddressByName(Document document, String elementName) { Element root = document.getRootElement(); Iterator<Object> 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; } } return document; } /** * 删除属性等于某个值的元素 * @param document XML文档 * @param xpath xpath路径表达式 * @param attrName 属性名 * @param attrValue 属性值 * @return */ @SuppressWarnings("unchecked") public static Document deleteElementByAttribute(Document document, String xpath, String attrName, String attrValue) { Iterator<Object> 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); } } return document; }}
0 0
- dom4j修改,获取,增加xml中某个元素的属性值
- dom4j修改,获取,增加xml中某个元素的属性值
- LINQ To XML:获取,修改,增加,删除元素的属性
- dom4j读取某个元素的某个属性
- dom4j 读取某个元素的某个属性
- dom4j读取某个元素的某个属性
- dom4j学习笔记[3]---获取、修改XML元素
- 封装获取某个元素指定css属性值的方法
- dom4j解析xml获取attribute属性值
- 获取到某个元素的某个style中的属性。
- 根据数组中某个属性值 去除相同的元素
- MongoDB中修改集合中的某个元素的值
- Dom4j增加,修改,删除XML文件
- Dom4j增加,修改,删除XML文件
- java中dom4j解析xml文件怎么获取节点属性
- 取xml任意节点中某个属性值的方法
- 取任意xml节点中某个属性值的方法
- Jquery获取和修改dom元素的属性值
- IO流_打印流改进复制文本文件案例
- 博为峰JavaEE技术文章-MyBatis @many使用方法
- AsyncTask的使用
- vue.js关于Object.defineProperty的利用原理
- No plugin found for prefix 'war' in the current project and in the plugin groups
- dom4j修改,获取,增加xml中某个元素的属性值
- 《CSS权威指南第三版》第六章-文本属性的读书笔记
- 使用Notification
- 用于Activity管理和应用程序退出
- C primer plus C 第六章控制语句:循环 编程练习个人练习
- linux 工作调度
- To be the better one for programing
- JAVA scanner扫描器和BufferedReader流输入的用法
- (3)防火墙的应用----NAT和ACL