XML读写工具
|
import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.util.List; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerFactory; import javax.xml.transform.stream.StreamSource; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.dom4j.Attribute; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.io.DocumentResult; import org.dom4j.io.DocumentSource; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; /** * XML读写工具 * @author csx * */ public class XmlUtil { private static final Log logger=LogFactory.getLog(XmlUtil.class); public static final String CONFIG_FILE_NAME = "hurong.xml";// 系统配置文件名称 public static Document getSystemConfigXML(){ try{ Document document = null; File configFile = null; String configFilePath = Thread.currentThread().getContextClassLoader().getResource("").toURI().getPath()+"hurong.xml"; configFile = new File(configFilePath); SAXReader saxReader = new SAXReader(); document = saxReader.read(configFile); return document;} catch (Exception e) { e.printStackTrace(); return null; } } /** * * 获取第三方短信、身份证验证接口 * * * **/ public static Document getThridInterface(){ try{ Document document = null; File configFile = null; String configFilePath = Thread.currentThread().getContextClassLoader().getResource("").toURI().getPath()+"thridInterface.xml"; configFile = new File(configFilePath); SAXReader saxReader = new SAXReader(); document = saxReader.read(configFile); return document;} catch (Exception e) { e.printStackTrace(); return null; } } /** * 把Document对象转成XML String * @param document * @return */ public static String docToString(Document document) { String s = ""; try { ByteArrayOutputStream out = new ByteArrayOutputStream(); OutputFormat format = new OutputFormat(" ", true, "UTF-8"); XMLWriter writer = new XMLWriter(out, format); writer.write(document); s = out.toString("UTF-8"); } catch (Exception ex) { logger.error("docToString error:"+ex.getMessage()); } return s; } /** * 把XML String转成Document对象 * @param s * @return */ public static Document stringToDocument(String s) { Document doc = null; try { doc = DocumentHelper.parseText(s); } catch (Exception ex) { logger.error("stringToDocument error:"+ex.getMessage()); } return doc; } /** * 把Document对象转成XML对象 * @param document * @param filename * @return */ public static boolean docToXmlFile(Document document, String filename) { boolean flag = true; try { OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("UTF-8"); XMLWriter writer = new XMLWriter( new FileOutputStream(new File(filename)), format); writer.write(document); writer.close(); } catch (Exception ex) { flag = false; logger.error("docToXmlFile error:"+ex.getMessage()); } return flag; } /** * 把String XML转成XML文件 * @param str * @param filename * @return */ public static boolean stringToXmlFile(String str, String filename) { boolean flag = true; try { Document doc = DocumentHelper.parseText(str); flag = docToXmlFile(doc, filename); } catch (Exception ex) { flag = false; logger.error("stringToXmlFile error:"+ex.getMessage()); } return flag; } /** * 加载一个XML文件转成Document对象 * @param filename * @return */ public static Document load(String filename) { return load(new File(filename)); } public static Document load(File file){ Document document = null; try { SAXReader saxReader = new SAXReader(); saxReader.setEncoding("UTF-8"); document = saxReader.read(file); } catch (Exception ex) { logger.error("load XML File error:"+ex.getMessage()); } return document; } /** * 加载一个XML文件转成Document对象 * @param filename * @return */ public static Document load(String filename,String encode) { Document document = null; try { SAXReader saxReader = new SAXReader(); saxReader.setEncoding("encode"); document = saxReader.read(new File(filename)); } catch (Exception ex) { logger.error("load XML File error:"+ex.getMessage()); } return document; } /** * 通过流加载一个XML文档对象 * @param is * @return */ public static Document load(InputStream is){ Document document = null; try { SAXReader saxReader = new SAXReader(); //System.out.println("code:" + System.getProperty("file.encoding")); saxReader.setEncoding("UTF-8"); document = saxReader.read(is); } catch (Exception ex) { logger.error("load XML File error:"+ex.getMessage()); } return document; } /** * 通过流加载一个XML文档对象 * @param is * @return */ public static Document load(InputStream is,String encode){ Document document = null; try { SAXReader saxReader = new SAXReader(); saxReader.setEncoding(encode); document = saxReader.read(is); } catch (Exception ex) { logger.error("load XML File error:"+ex.getMessage()); } return document; } public static Document styleDocument( Document document, String stylesheet ) throws Exception { // load the transformer using JAXP TransformerFactory factory = TransformerFactory.newInstance(); Transformer transformer = factory.newTransformer( new StreamSource( stylesheet ) ); // now lets style the given document DocumentSource source = new DocumentSource( document ); DocumentResult result = new DocumentResult(); transformer.transform( source, result ); // return the transformed document Document transformedDoc = result.getDocument(); return transformedDoc; } public static void main(String[]args){ String filePath="L:/devtools/workspace/eoffice/web/js/menu.xml"; String style="L:/devtools/workspace/eoffice/web/js/menu-public.xsl"; Document doc=XmlUtil.load(filePath); try{ Document another=styleDocument(doc,style); System.out.println("xml:" + another.asXML()); //Set idSet=new HashSet(); Document publicDoc=another; Element rootEl=publicDoc.getRootElement(); List idNodes=rootEl.selectNodes("/Menus//*"); System.out.println("size:" + idNodes.size()); for(int i=0;i<idNodes.size();i++){ Element el=(Element)idNodes.get(i); Attribute attr= el.attribute("id"); if(attr!=null){ System.out.println("attr:" + attr.getValue()); //idSet.add(attr.getValue()); } } }catch(Exception ex){ ex.printStackTrace(); } } } |
XML读写工具的更多相关文章
- XML读写工具类
摘要:①读取XML文件,生成pojo对象:②将对象信息保存到xml中. 步骤: ①新建一个普通的java类BasePage: package com.test.selenium.pages; impo ...
- 【Python】Python XML 读写
class ACTIVE_FILE_PROTECT_RULE_VIEW(APIView): renderer_classes = (JSONRenderer, BrowsableAPIRenderer ...
- Java XML解析工具 dom4j介绍及使用实例
Java XML解析工具 dom4j介绍及使用实例 dom4j介绍 dom4j的项目地址:http://sourceforge.net/projects/dom4j/?source=directory ...
- XML编辑工具
[标题]XML编辑工具 [开发环境]Qt 5.2.0 [概要设计]使用QT的视图/模型结构.treeview控件以树形结构显示所要操作的XML文件,并实现xml的相关操作 [详细设计] 主要包含 no ...
- ntfs读写工具Paragon NTFS 15无限使用教程
Paragon NTFS mac版是Mac OS平台上最受欢迎的ntfs读写工具,专门开发用来弥补Windows和Mac OS X之间的不兼容性,通过在Mac OS X系统下提供对任何版本的NTFS文 ...
- properties文件读写工具类
java代码: import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; ...
- 在Android源码树中添加userspace I2C读写工具(i2c-util)
在Android源码树中添加userspace I2C读写工具(i2c-util) http://blog.csdn.net/21cnbao/article/details/7919055 分类: A ...
- jsonUtils&&Json、Xml转换工具Jackson使用
1.jsonUtils package com.icil.utils; import java.util.List; import com.fasterxml.jackson.core.JsonPro ...
- Spring-Boot ☞ ShapeFile文件读写工具类+接口调用
一.项目目录结构树 二.项目启动 三.往指定的shp文件里写内容 (1) json数据[Post] { "name":"test", "path&qu ...
随机推荐
- SqlServer中的更新锁(UPDLOCK和READPAST)
UPDLOCK和READPAST,通过UPDLOCK和READPAST的结合我们能够解决许多问题,比如我当前项目中对于更新预约人数,则用到了UPDLOCK和READPAST,因为考虑到并发如果固定预约 ...
- django项目用higcharts统计最近七天文章点击量。
下载higcharts插件放在static文件夹下 前端引入 <script src="/static/highcharts/highcharts.js"></s ...
- BFS (1)算法模板 看是否需要分层 (2)拓扑排序——检测编译时的循环依赖 制定有依赖关系的任务的执行顺序 djkstra无非是将bfs模板中的deque修改为heapq
BFS模板,记住这5个: (1)针对树的BFS 1.1 无需分层遍历 from collections import deque def levelOrderTree(root): if not ro ...
- nameof 运算符
0. 目录 C#6 新增特性目录 1. 老版本的代码 1 using System; 2 namespace csharp6 3 { 4 internal class Program 5 { 6 pr ...
- 图论 - 二分图的判断(dfs染色法)
二分图的判断(dfs染色法) 如何判断一个图是否为二分图 普通染色法模板 C++ 代码模板如下 思想:先将当前点染色,然后再将该点相连的结点进行染另外一种颜色 下面附上自己画的一张图假设我们从第一个点 ...
- Mysql的安装与环境的配置
Mysql的安装与环境的配置 这里以Mysql5.5为例: (1)双击安装包,点击next (2)选择自定义,点击Next (3)修改路径,点击Next (4)选择精确配置,点击Next (5)选择开 ...
- (尚034)Vue_案例_数据存储优化(代码优化!!!)
最好能将上述代码抽取成一个模块(读json数据+写json数据) 1.在src下新建文件夹util(util文件夹用于放入工具的模块) 2.*使用localStorage存储数据的工具模块* 一个模块 ...
- HustOJ二次开发之修改相关Logo
比如将如图中的HUSTOJ进行修改: 在Linux上修改,通过关键字搜索,会获取如下两个重要文件,找到都有的文字进行修改即可: grep -rn "HUSTOJ" * cd /ho ...
- redis(三) 集群 codis
参考文档 http://blog.csdn.net/ztsinghua/article/details/48134377
- Octopus501工作站 安装记录
cmake libreadline-dev 没有运行程序,nvidia-smi查看GPU-Util 达到100% 解决方案:需要把驱动模式设置为常驻内存才可以,设置命令:nvidia-smi -pm ...