public static void main(String[] args) {
SAXReader saxReader = new SAXReader();
Document document;
try {
document = saxReader.read(new File(Dom4j.class.getClass()
.getResource("/").getFile().toString()
+ "test.xml"));
Element root = document.getRootElement();
Element node = Dom4j.parse(root , "name" , "b");
...
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/*
* 获得X属性结果是X值的整个标签
*/
public static Element parse(Element node , String type , String val) {
for (Iterator iter = node.elementIterator(); iter.hasNext();) {
Element element = (Element) iter.next();
Attribute name = element.attribute(type);
if (name != null) {
String value = name.getValue();
if (value != null && val.equals(value))
return element;
else
parse(element , type , val);
}
}
return null;
}