用到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"));
}
}
}