dom4j修改,获取,增加xml中某个元素的属性值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”
/
/ 获取XML
Document document = Document(xmlPath);
Element root = RootElement();
Iterator<?> ruleNodes = root.elementIterator("item");
while (ruleNodes.hasNext()) {
Element ruleElement = (Element) ();
// 修改isSelf的属性值
Attribute isSelfAttr = ruleElement.attribute("isSelf");
isSelfAttr.setValue("n");
}
writeXml(document, Path());
/
**
* 输出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 (ists())
xmlFile.delete();
writer = new XMLWriter(new FileOutputStream(xmlFile), atePrettyPrint());
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 (!ists()) {
return null;
} else {
ad(file);
}
} catch (DocumentException e) {
throw new RuntimeException(e + "->指定⽂件【" + xmlFile + "】读取错误");        }
}
/**
* 得到XML⽂档(编码格式-gb2312)
*
* @param xmlFile
* @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 (!ists()) {
return null;
} else {
ad(file);
}
} catch (DocumentException e) {
throw new RuntimeException(e + "->指定⽂件【" + xmlFile + "】读取错误");
}
}
public static String getText(Element element) {使用dom4j解析xml文件
try {
TextTrim();
} catch (Exception e) {
throw new RuntimeException(e + "->指定【" + Name() + "】节点读取错误");
}
}
/**
* 增加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 = RootElement();
Element subElement = root.addElement(elementName);
for (Map.Entry<String, String> attr : Set()) {
subElement.Key(), Value());
}
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 = RootElement();
Iterator<Object> iterator = root.elementIterator("file");
while (iterator.hasNext()) {
Element element = (Element) ();
// 根据属性名获取属性值
Attribute attribute = element.attribute("name");
Attribute attribute = element.attribute("name");
if (Value().equals(elementName)) {
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 (ists())
xmlFile.delete();
writer = new XMLWriter(new FileOutputStream(xmlFile), atePrettyPrint());
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 = ateDocument();
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 = RootElement();
Iterator<Object> iterator = root.elementIterator("address");
while (iterator.hasNext()) {
while (iterator.hasNext()) {
Element element = (Element) ();
// 根据属性名获取属性值
Attribute attribute = element.attribute("name");
if (Value().equals(elementName)) {
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) ();
Element parentElement = Parent();
// 根据属性名获取属性值
Attribute attribute = element.attribute(attrName);
if (Value().equals(attrValue)) {
}
}
return document;
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。