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;
}
}