W3CDom操作XML文档实用工具类

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;
	}

}

猜你喜欢

转载自blog.csdn.net/cgs666/article/details/50848003