package org.jdom;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.io.FileUtils;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import org.jdom.xpath.XPath;
import thtf.ebuilder.Config;
/**
* XML简化操作类
*
* @author kettas 3:11:54 PM
*/
public class XML {
private static Document document = null;
/**
* 是否存在此文件 <br>
* 如果不存在就抛出异常,存在就返回此文件
*
* @param file 文件对象
* @return File
* @throws IOException
*/
protected static File readFile(File file) throws IOException {
try {
if (file.exists()) {
return file;
}
} catch (Exception e) {
throw new IOException("找不到文件" + e.getMessage());
}
throw new IOException("找不到文件");
}
/**
* <pre>
* 将文件转换为Document对象
* <code>Document doc=XML.fileToDocument(new File("c://abc/a.xml"));</code>
* </pre>
* @param file 文件路径
* @return Document
* @throws JDOMException
*/
public static Document fileToDocument(File file) throws JDOMException {
try {
document = new SAXBuilder(false).build(readFile(file));
return document;
} catch (IOException e) {
document=null;
e.printStackTrace();
throw new JDOMException("Can't find ["+file.getPath()+"]file !");
}catch (Exception e) {
document=null;
throw new JDOMException(e.getMessage());
}
}
/**
* <pre>自定义表达式的方式来读取配置文件信息:并指定你需要操作的文件的路径
* <code>List list=XML.fileToDocument(new File("c://abc/a.xml"));</code></pre>
* @param file xml 文件路径
* @param express xpath查询表达式
* @return List<Element>
* @throws JDOMException
*/
public static List queryToElementList(File file, String express)
throws JDOMException {
return queryToElementList(file, XPath.newInstance(express));
}
/**
* 根据您的设置的Xpath语法查询XML文件并返回查询结果
* <pre>
* File file=new File("c:\\rss.xml");
* List list=XML.queryToElementList(file,XPath.newInstance("/rss/channel/title"));
* for(Element e:list){
* System.out.println("名称:"+e.getText());
* }
* </pre>
* @param file
* @param xPath
* @return List
* @throws JDOMException
*/
public static List queryToElementList(File file, XPath xPath)
throws JDOMException {
return xPath.selectNodes(fileToElement(file));
}
/**
* 根据您的设置的Xpath语法查询Document并返回查询结果
* <pre>
* Document doc=XML.fileToDocument(new File("c:/xml.xml"));
* List list=org.jdom.XML.queryToElementList(doc,"/rss/channel/title");
* for(Element e:list){
* System.out.println("名称:"+e.getText());
* }
* </pre>
* @param doc
* @param express
* @return List
* @throws JDOMException
*/
public static List queryToElementList(Document doc, String express)
throws JDOMException {
return XPath.newInstance(express).selectNodes(doc);
}
/**
* 查询文件顶层节点.
* @param file 文件路径
* @return Element
* @throws JDOMException
*/
public static Element fileToElement(File file) throws JDOMException{
return fileToDocument(file).getRootElement();
}
/**
* 查询文件所有节点.
* @param file
* @return List
* @throws JDOMException
*/
public static List fileToElementList(File file)
throws JDOMException {
return fileToElement(file).getChildren();
}
/**
* 自定义Xpath查询表达式,查询指定的文件并简单封装为List of Map,并返回.
* @deprecated
* @param file 需要查询的文件路径
* @param express xpath查询表达式
* @return List
* @throws JDOMException
*/
public static List<Map<String, String>> queryToMapList(File file,
String express) throws JDOMException {
return elementToMap(queryToElementList(file, express));
}
/**
* 自定义Xpath查询表达式,查询指定的文件并简单封装为List of Map,并返回.
* @deprecated
* @param file 需要查询的文件路径
* @param express xpath查询表达式
* @return List
* @throws JDOMException
*/
public static List<Map<String, String>> queryAttributeToMapList(File file,
String express) throws JDOMException {
return elementAttributeToMap(true,queryToElementList(file, express));
}
/**
* 将节点封装成为List of Map.不推荐使用.(不支持节点属性,只支持节点)
* @deprecated
* @param elementList
* @return List
*/
public static List<Map<String, String>> elementToMap(
List elementList) {
return elementAttributeToMap(false,elementList);
}
private static Map toMap(boolean readAttribute,Element element){
int j=0;
Map<String, String> map = new HashMap<String, String>();
//读取所有属性
List<Attribute> as=element.getAttributes();
for (j=0;readAttribute&&as!=null&&j<as.size();j++) {
Attribute _as=as.get(j);
map.put(_as.getName(), element.getAttributeValue(_as.getName()));
}
//读取所有子节点
List listElement = element.getChildren();
for (j=0;listElement!=null&&j<listElement.size();j++) {
Element element2=(Element)listElement.get(j);
map.put(element2.getName(), element2.getText());
}
return map;
}
public static List<Map<String, String>> elementAttributeToMap(boolean readAttribute,
List elementList) {
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
for (int i=0,j=0;list!=null&&i<elementList.size();i++) {
Element element=(Element)elementList.get(i);
list.add(toMap(readAttribute,element));
}
return list;
}
/**
* 将Document对象保存为一个xml文件
* <pre>
* Document doc=new Document();
* Element e=new Element("r");
* doc.setRootDocument(e);
* XML.documentToFile(doc,new File("c://abc/a.xml"));</pre>
* @param document 对象
* @param saveFile 文件保存地址
* @return boolean
*/
public static boolean documentToFile(Document document, File saveFile) {
try {
if(!saveFile.getParentFile().exists()){
FileUtils.forceMkdir(saveFile.getParentFile());
}
FileOutputStream out1 = new FileOutputStream(saveFile);
Format format = Format.getPrettyFormat();
format.setEncoding(Config.DEFAULT_TYPE);
format.setIndent("\t");
XMLOutputter outputter = new XMLOutputter(format);
outputter.output(document, out1);
out1.flush();
out1.close();
return true;
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
/**
* 将表达式查出的节点全部删除,并保存。
* <pre>
* XML.remove(new File("c://abc/a.xml"),"/xml");</pre>
* </pre>
* @param file XML文件路径
* @param express Xpath查询表达式
* @return boolean
* @throws JDOMException
*/
public static boolean remove(File file, String express)
throws JDOMException {
List eList = queryToElementList(file, express);
for (int i=0;eList!=null&&i<eList.size();i++) {
Element element =(Element)eList.get(i);
element.removeContent();
}
return documentToFile(document, file);
}
}