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 发布