public class XmlUtils {
/**
* 表达式的格式如:project.profiles.profile,返回最后一个定位到的单个标签
*
*/
public static Element getElement(Element ele, String exp) {
String[] tags = exp.split("\\.");
for (String tag : tags) {
NodeList nodeList = ele.getElementsByTagName(tag);
if (nodeList.getLength() > 0) {
ele = (Element) nodeList.item(0);
} else {
return null;
}
}
return ele;
}
/**
* 表达式的格式如:project.profiles.profile,返回最后一个定位到的标签列表
*
*/
public static NodeList getElements(Element ele, String exp) {
String[] tags = exp.split("\\.");
NodeList nodeList = null;
for (String tag : tags) {
nodeList = ele.getElementsByTagName(tag);
if (nodeList.getLength() == 0) {
return null;
}
ele = (Element) nodeList.item(0);
}
return nodeList;
}
}W3CDom操作XML文档实用工具类
最新推荐文章于 2023-04-26 16:45:15 发布
本文介绍了一个用于解析XML文档并根据指定路径获取元素的实用工具类。该工具支持两种查询方式:一种是返回单个匹配元素,另一种是返回所有匹配元素的集合。这两种方法都接受一个表达式参数,该参数定义了要查询的元素路径。
1187

被折叠的 条评论
为什么被折叠?



