日期:2014-05-20 浏览次数:20797 次
public static List getElementsByName(Document document, String name) {
List list = document.selectNodes("//" + name);
return list;
}
import org.dom4j.*;
import org.dom4j.io.SAXReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class XmlAndXPath {
public static void main(String[] args) throws Exception {
SAXReader reader = new SAXReader();
Document doc = reader.read("/Users/Biao/Desktop/feature.xml");
List<String> ids = new ArrayList<String>();
XPath xpathSelector = DocumentHelper.createXPath("//plugin[@id]");
List nodes = xpathSelector.selectNodes(doc);
for (Object obj : nodes) {
Element n = (Element) obj;
ids.add(n.attribute("id").getText());
}
Collections.sort(ids);
for (String id : ids) {
System.out.println(id);
}
}
}