读书人

JDOM 打包XML操作

发布时间: 2012-08-11 20:50:31 作者: rapoo

JDOM 封装XML操作

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.XMLOutputter;import org.jdom.xpath.XPath;/** * XML简化操作类 *  * @author 杨伦亮 3:11:54 PM */public class XML {private static Document document = null;/** * 是否存在此文件 <br> * 如果不存在就抛出异常,存在就返回此文件 *  * @param path * @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("找不到文件");}/** * 添加节点 * @param file * @param element * @param express * @return boolean */public static boolean appendElement(File file,Element element,String express){try{List<Element> list=XPath.newInstance(express).selectNodes(fileToElement(file));}catch (Exception e) {}return false;}/** * 读取doucment *  * @param filePath * @return Document * @throws JDOMException */public static Document fileToDocument(File file) throws JDOMException {try {document = new SAXBuilder().build(readFile(file));return document;} catch (JDOMException e) {throw e;} catch (IOException e) {throw new JDOMException("Can't find ["+file.getPath()+"]file !");}}/** * 自定义表达式的方式来读取配置文件信息:并指定你需要操作的文件的路径 *  * @param path *            xml 文件路径 * @param express * @return List<Element> * @throws JDOMException */@SuppressWarnings("unchecked")public static List<Element> queryToElementList(File file, String express)throws JDOMException {return XPath.newInstance(express).selectNodes(fileToElement(file));}public static Element fileToElement(File file) throws JDOMException{return fileToDocument(file).getRootElement();}@SuppressWarnings("unchecked")public static List<Element> fileToElementList(File file)throws JDOMException {return fileToElement(file).getChildren();}public static List<Map<String, String>> queryToMapList(File file,String express) throws JDOMException {return elementToMap(queryToElementList(file, express));}@SuppressWarnings("unchecked")public static List<Map<String, String>> elementToMap(List<Element> elementList) {List<Map<String, String>> list = new ArrayList<Map<String, String>>();for (Element element : elementList) {List<Element> listElement = element.getChildren();for (Element element2 : listElement) {Map<String, String> map = new HashMap<String, String>();map.put(element2.getName(), element2.getText());list.add(map);}}return list;}/** *  将Document对象保存为一个xml文件到本地 *  * @return true:保存成功 flase:失败 * @param filename *            保存的文件名 * @param document *            需要保存的document对象 */public static boolean documentToFile(Document document, File tagFile) {try {new XMLOutputter().output(document, new FileOutputStream(tagFile));return true;} catch (Exception e) {e.printStackTrace();}return false;}public static boolean remove(File file, String express)throws JDOMException {List<Element> eList = queryToElementList(file, express);for (Element element : eList) {element.removeContent();}return documentToFile(document, file);}public static void main(String[] args) {try {File file = new File("c://Untitled-1.html");String express = "/html/body/div";//remove(file, express);List<String> list=FileUtils.readLines(file, "utf-8");for (String s:list) {System.out.println(s);}} catch (Exception e) {e.printStackTrace();}}}

?

读书人网 >XML SOAP

热点推荐