XML解析工具类
public class XmlUtil { /*
* 利用dom4j解析xml文件内容,并返回map数据形式
* path是.xml文件所在的路径
*/
public static Map<String,String> paserXmlByDOM4J(String path) throws Exception{
path=ServletActionContext.getServletContext().getRealPath(path);
SAXReader reader = new SAXReader();
Document doc = reader.read(new FileInputStream(new File(path)));
Map<String,String> xml=paserXml(doc);
return xml;
}
//传入xml格式的string,转化为xml类型,然后解析其内容,返回map数据形式
/*
* strXml是xml格式的字符串
*/
public static Map<String,String> strToXmlAndPaserXml(String strXml) throws Exception{
SAXReader reader = new SAXReader();
Document doc = reader.read(new ByteArrayInputStream(strXml.getBytes("UTF-8")));
Map<String,String> xml=paserXml(doc);
return xml;
}
//遍历解析xml数据
public static Map<String,String> paserXml(Document doc) throws Exception{
Map<String,String> xml=new HashMap<String,String>();
Element root = doc.getRootElement();
Iterator it = root.elementIterator();
Element element;
while (it.hasNext()) {
element = (Element) it.next();
xml.put(element.getName(),element.getText());
}
return xml;
}
}
XML解析工具类的更多相关文章
- java自带的xml解析工具类
public class JaxbUtil { /** * java对象转换为xml文件 * * @param xmlPath xml文件路径 * @param load java对象.Class * ...
- Java XML解析工具 dom4j介绍及使用实例
Java XML解析工具 dom4j介绍及使用实例 dom4j介绍 dom4j的项目地址:http://sourceforge.net/projects/dom4j/?source=directory ...
- JSON对象与XML相互转换工具类
依赖jar <dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId ...
- PHP 命令行参数解析工具类
<?php/** * 命令行参数解析工具类 * @author guolinchao * @email luoyecb@163.com */class CommandLine{ // store ...
- 读取EXCEL文档解析工具类
package test;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException ...
- 阶段3 1.Mybatis_03.自定义Mybatis框架_4.自定义mybatis的编码-解析XML的工具类介绍
导入xml操作的类和用到的相关包 创建util包,然后把提供好的XMLConfigBuilder.java文件复制3过来 复制过来,里面用到了很多dom4j的东西 打开pom.xml 输入depend ...
- NSXMLParser自定义的一个xml解析工具
// // DenglXMLParser.h // #import <Foundation/Foundation.h> @interface DenglXMLParser : NSXMLP ...
- 一个强大的json解析工具类
该工具类利用递归原理,能够将任意结构的json字符串进行解析.当然,如果需要解析为对应的实体对象时,就不能用了 package com.wot.cloudsensing.carrotfarm.util ...
- XML读写工具类
摘要:①读取XML文件,生成pojo对象:②将对象信息保存到xml中. 步骤: ①新建一个普通的java类BasePage: package com.test.selenium.pages; impo ...
随机推荐
- HDU 5410 CRB and His Birthday ——(完全背包变形)
对于每个物品,如果购买,价值为A[i]*x+B[i]的背包问题. 先写了一发是WA的= =.代码如下: #include <stdio.h> #include <algorithm& ...
- 关于python中带下划线的变量和函数 的意义
总结: 变量: 1. 前带_的变量: 标明是一个私有变量, 只用于标明, 外部类还是可以访问到这个变量 2. 前带两个_ ,后带两个_ 的变量: 标明是内置变量, 3. 大写加下划线的变量: ...
- Unable to require openssl, install OpenSSL and rebuild ruby (preferred) or use non-HTTPS sources解决
解决方法 ruby -v rvm requirements brew install libyaml rvm pkg install openssl rvm install 2.3.1 --with- ...
- miniui中常用的状态显示方式
1.查询sys_code表得到对应的状态 考生状态:<input class="mini-combobox" style="" textField=&qu ...
- 后台dom拼接xml,动态显示统计图
这个东西让我好生头疼,贴代码吧 // 两秒后模拟点击 setTimeout(function() { // IE if(document.all) { document.getElementById( ...
- 利用Unity制作“表”
一枚小菜鸟 目前没发现在Unity有其他路径制作类似于c# WinForm中的表:但是利用Unity自带的UGUI,制作了一张"伪表",具体方案如下: 效果图如下: 步骤: 1 ...
- sublime text3的安装与汉化
sublime text3 3126 64位的下载地址: https://download.sublimetext.com/Sublime%20Text%20Build%203126%20x64%20 ...
- asp.net web api添加自定义认证
1.定义认证失败结果生成器 /// <summary> /// 认证失败结果生成器 /// </summary> public class AuthenticationFail ...
- 用js将从后台得到的时间戳(毫秒数)转换为想要的日期格式
得到后台从数据库中拿到的数据我们希望格式是 2016年10月25日 17时37分30秒 或者 2016/10/25 17:37:30 然而我们前台得到的却是一段数字(时间戳,毫秒数) 14773860 ...
- OpenSource.organization-in-github
1. gosquared https://github.com/gosquared 2. slack https://github.com/slackhq 3. The New York Times ...