利用Xpath快速定位元素

package cn.itcast.xml.parser.xpath;

import java.io.File;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class Demo1 {
	public static void main(String[] args) throws Exception {
		SAXReader reader = new SAXReader();
		Document document = reader.read(new File("src/cn/itcast/xml/parser/xpath/car.xml"));
		String xpath = "//单价";
		Element priceElement = (Element) document.selectSingleNode(xpath);
		String priceName = priceElement.getName();
		String priceText = priceElement.getText();
		System.out.println(priceName + ":" + priceText);
	}
}









 

你可能感兴趣的:(xml)