dom4j - xpath

SAXReader reader = new SAXReader();
Document doc = reader.read(new File("file path"));

testXOPathNode(doc);
testXPathAttribute(doc);


	private static void testXOPathNode(Document doc) {
		List list = doc.selectNodes("/node/dependency");

		Iterator iter = list.iterator();
		while (iter.hasNext()) {
			Element element = (Element) iter.next();
			System.out.println(element.getName());
		}
	}

	private static void testXPathAttribute(Document doc) {
		List list = doc.selectNodes("/node/dependency/@name");

		Iterator iter = list.iterator();
		while (iter.hasNext()) {
			Attribute attr = (Attribute) iter.next();
			System.out.println(attr.getValue());
		}
	}


dom4j-x.x.x.jar & jaxen-x.x.x.jar are required.
ClassNotFoundException will occur if you miss jaxen-x.x.x.jar

你可能感兴趣的:(java)