Dom4j根据条件解析xml

用到dom4j.jar

工具类:

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.DocumentHelper;
/**
 * 针对XML的解析
 * 筛选符合条件的element
 * 条件:elementName、attrName、attrValue
 * */
public class XMLUtil {
	public static String elementName;
	public static String attrName;
	public static String attrValue;

	/**
	 * 解析文件,获得符合条件的element
	 * */
	public static List<Element> interceptElements(File file) throws DocumentException{
		if (elementName==null || "".equals(elementName.trim())) {
			return null;
		}
		if(attrName==null || "".equals(attrName.trim()) || 
			attrValue==null || "".equals(attrValue.trim())){
			attrName = "";
			attrValue = "";
		}
		SAXReader reader = new SAXReader();
		Document doc = reader.read(file);
		Element root = doc.getRootElement();
		List<Element> elements = new ArrayList<Element>();
		parse(elements,root);
		return elements;
	}
	/**
	 *解析字符串,获得符合条件的element
	 * */
	public static List<Element> interceptElements(String xml) throws DocumentException{
		if (elementName==null || "".equals(elementName.trim())) {
			return null;
		}
		if(attrName==null || "".equals(attrName.trim()) || 
			attrValue==null || "".equals(attrValue.trim())){
			attrName = "";
			attrValue = "";
		}
		Document doc = DocumentHelper.parseText(xml);
		Element root = doc.getRootElement();
		List<Element> elements = new ArrayList<Element>();
		parse(elements,root);
		return elements;
	}
	/**
	 * 解析输入流,获得符合条件的element
	 * */
	public static List<Element> interceptElements(InputStream in) throws DocumentException{
		if (elementName==null || "".equals(elementName.trim())) {
			return null;
		}
		if(attrName==null || "".equals(attrName.trim()) || 
			attrValue==null || "".equals(attrValue.trim())){
			attrName = "";
			attrValue = "";
		}
		SAXReader reader = new SAXReader();
		Document doc = reader.read(in);
		Element root = doc.getRootElement();
		List<Element> elements = new ArrayList<Element>();
		parse(elements,root);
		return elements;
	}
	/**
	 * 递归扫描整个xml
	 * */
	private static void parse(List<Element> list,Element element){
		List<Element> childs = element.elements();
		for (int i = 0; i < childs.size();i++) {
			Element e = childs.get(i); 
			if (elementName.equals(e.getName())) {
				if("".equals(attrName)){
					list.add(e);
				}else if(attrValue.equals(e.attributeValue(attrName))){
					list.add(e);
				}
				continue;
			}
			if (e.elements().size()>0) {
				parse(list,e);
			}
		}
	}
}

 testDemo:我解析的是html中的hidden控件

public class XMLUtilDemo {
	public static void main(String[] args) {
			XMLUtil.elementName = "input";
			XMLUtil.attrName = "type";
			XMLUtil.attrValue = "hidden";
			
			List<Element> elements = null;
			try {
				elements = XMLUtil.interceptElements(XMLUtilDemo.class.getResourceAsStream("../domTest.html"));
			} catch (DocumentException e) {
				System.out.println(e.getMessage());
			}
			
			if (elements == null) 
				return;
			
			for (Element element : elements) {
				System.out.println(element.asXML());
				//System.out.println(element.attributeValue("name")+"= "+element.attributeValue("value"));
			}
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值