没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
java操作操作xml的方法汇总及解析的方法汇总及解析
主要介绍了java操作xml的方法汇总及解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定
的参考学习价值,需要的朋友可以参考下
这篇文章主要介绍了java操作xml的方法汇总及解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参
考学习价值,需要的朋友可以参考下
一丶常用方法一丶常用方法
主要有3个方面, 1读取xml文件, 2使用xpath根据指定路径获取某一节点数据 3, xml和java bean的转换
XmlUtils.java
/**
* 和cn.hutool.core.util.XmlUtil许多功能重合, 本类可以当做学习的例子
* 可以直接使用cn.hutool.core.util.XmlUtil
*
* @author TimFruit
* @date 19-11-2 下午5:22
*/
public class XmlUtils {
// --------------------------------------
public static Document createXml(){
return XmlUtil.createXml();
}
// --------------------------------------
/**
* 读取xml文档
* @param xmlInputStream
* @return
*/
public static Document readXml(InputStream xmlInputStream){
return readXml(xmlInputStream, false);
}
public static Document readXml(InputStream xmlInputStream, boolean validate){ // 参考mybatis parsing模块
try {
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
factory.setValidating(validate);
factory.setNamespaceAware(false);
factory.setIgnoringComments(true);
factory.setIgnoringElementContentWhitespace(false);
factory.setCoalescing(false);
factory.setExpandEntityReferences(true);
DocumentBuilder builder=factory.newDocumentBuilder();
return builder.parse(xmlInputStream);
} catch (ParserConfigurationException e) {
throw new RuntimeException(e);
} catch (SAXException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public static Document readXml(String xmlStr){
return XmlUtil.parseXml(xmlStr); //使用hutool
}
// --------------------------------------
// 根据路径获取某一节点
public static XPath newXpath(){
return XPathFactory.newInstance().newXPath();
}
/**
* 根据路径获取某一节点, 语法看 https://www.w3school.com.cn/xpath/xpath_syntax.asp
* @param expression
* @param root 可以是document, 可以是Node等其他节点
* @param xpath
* @return 返回的节点可以修改
*/
public static Node evalNode(String expression, Object root, XPath xpath){
资源评论
weixin_38644168
- 粉丝: 0
- 资源: 896
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功