日期:2014-05-19 浏览次数:20696 次
import org.dom4j.*; import org.dom4j.io.SAXReader; import java.util.List; public class XmlDemo { public static void main(String[] args) throws Exception { SAXReader reader = new SAXReader(); Document doc = reader.read("demo.xml"); // device[@user_agent] // 查找有user_agent属性的device节点 XPath xpathSelector = DocumentHelper.createXPath("//device[@user_agent]"); List nodes = xpathSelector.selectNodes(doc); for (Object obj : nodes) { Element n = (Element) obj; System.out.println(n.attribute("user_agent").getText()); } } }