XPath 入门

引用
xpath可以快速定位获取XML文件中指定属性和值,jdk包含拉所有需要的类


1. 帮助类
public class XpathUtil {
	private static final Log logger = LogFactory.getLog(XpathUtil.class.getName());
	private Document doc;

	public XpathUtil(File file) {
		DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
		domFactory.setIgnoringElementContentWhitespace(true);
		domFactory.setNamespaceAware(false);
		try {
			DocumentBuilder builder = domFactory.newDocumentBuilder();
			this.doc = builder.parse(file);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public List<String> getValuesByXpath(String expString) {
		List<String> values = new ArrayList<String>();

		try {
			String expArray[]= expString.split(Constants.COMMA);
			for(String exp :expArray){
				XPath xpath = XPathFactory.newInstance().newXPath();
				
				XPathExpression expr = xpath.compile(exp);
				Object result = expr.evaluate(this.doc, XPathConstants.NODESET);
				NodeList nodes = (NodeList) result;
				for (int i = 0; i < nodes.getLength(); i++) {
					String nodeValue = nodes.item(i).getTextContent();
					values.add(nodeValue);
				}
				
			}
			
		} catch (XPathExpressionException e) {
			logger.error("", e);
		} catch (DOMException e) {
			logger.error("", e);
		}
		return values;
	}

}


2. XML Sample
<?xml version="1.0" encoding="UTF-8"?>
<p:ProcessLifeCycle xmlns:p="http://www.example.org/draft_2/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.example.org/draft_2/ metaData.xsd ">
   
   <TableList>
   		<Table>
			<name>tab.eq_exec_detail</name>	
			<p:CommonRefAttrElem>
				<name>table_name</name>
				<value>eq_exec_detail</value>
			</p:CommonRefAttrElem>			 
			<p:CommonRefAttrElem>
				<name>primary_key</name>
				<value>exec_did</value>
			</p:CommonRefAttrElem>	
			<p:CommonRefAttrElem>
		       	<name>record</name>
		        <record>
		        	<name>rec.eq_exec_detail</name>
		        	<description>exec detail record in ocean format. Table tab.eq_exec_detail</description>
		        	<p:CommonRefAttrElem>
		        		<name>exec_did</name>
						<type>bigint</type>	
						<length>8</length>			
						<value>not_possdup</value>	
						<nullFlag>N</nullFlag>
		        	</p:CommonRefAttrElem>
		        	<p:CommonRefAttrElem>
		        		<name>sys_exec_ver</name>
						<type>integer</type>
						<length>4</length>					
						<value>possdup</value>	
						<nullFlag>Y</nullFlag>
		        	</p:CommonRefAttrElem>
		        	<p:CommonRefAttrElem>
		        		<name>side</name>
						<type>varchar</type>
						<length>20</length>					
						<value>possdup</value>
						<nullFlag>Y</nullFlag>	
		        	</p:CommonRefAttrElem>
		        </record>
      		</p:CommonRefAttrElem>
						
		</Table>
</TableList>    
</p:ProcessLifeCycle>


3. 测试类
public class XpathUtilTest {
	@Test
	public void testRetrieveXpath() throws Exception {
		XpathUtil util = new XpathUtil(new File("test.xml"));
		List<String> values = util.getValuesByXpath("//TableList/Table/CommonRefAttrElem/record/CommonRefAttrElem/name");
		System.out.println(values);
	}
}


输出
引用
[exec_did, sys_exec_ver, side]


4. xpath获取eclipse插件,解压到dropin文件夹。
http://code.google.com/p/eclipse-xpath-evaluation-plugin/downloads/detail?name=eclipse-xpath-evaluation-plugin-1.2.3.zip

点击xml节点,可以获取XPATH

猜你喜欢

转载自caerun.iteye.com/blog/1699058