由于DOM4J在解析XML时只能一层一层解析,所以当XML文件层数过多时使用会很不方便,结合XPATH就可以直接获取到某个元素
使用dom4j支持xpath的操作的几种主要形式
第一种形式
/AAA/DDD/BBB: 表示一层一层的,AAA下面 DDD下面的BBB
第二种形式
//BBB: 表示和这个名称相同,表示只要名称是BBB,都得到
第三种形式
/*: 所有元素
第四种形式
BBB[1]: 表示第一个BBB元素
BBB[last()]:表示最后一个BBB元素
第五种形式
//BBB[@id]: 表示只要BBB元素上面有id属性,都得到
第六种形式
//BBB[@id='b1'] 表示元素名称是BBB,在BBB上面有id属性,并且id的属性值是b1
使用dom4j支持xpath具体操作
默认的情况下,dom4j不支持xpath,如果想要在dom4j里面是有xpath,第一步需要,引入支持xpath的jar包,如下:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAmoAAAAaCAIAAACIDefAAAAHuElEQVR4nO2d3W4TRxTH/Vp+g76IL/MClXpF5a0q1YogbbloWAgURGUDwXIZNgkECFRyACsSjdxKVYIUVzRFEPy5Xq+tXqztnY8z+xXH3pT/T3th787MOTOZnf+eM2vIuBEwbu81O/0oJQEAAIDF0uz0jdt7Z20lE6XQ/d/espdHPXswBAAAAFJMzx6wl0f3XrxNhXx2bae0c3jh1t5Xa69x4MCBAweO1B4Xbu2Vdg67tpMK+QQAAAAAT0L53Nbz6NGjjY2N2XoJAAAApIrk8ukOBq47GA7d4dAdjYaj0dB1B6PRsFqtWpZlbVizdRQAAABIDwnl8/Hjx4OBoyroaDR8/vx5t9tljG1tbc3WVwAAACAlJJTPra0tx+mTCmpZFmOMMWZZSgBaL2Qz2UL9tE7PhXohm8mVF2X5vIwSAJ8JDWYsmTX1fM0kT8+BBjOWDNZYqA+fNwnlc3Nz0+nbT17Xq/sHu/uHu/uH1d8PdvcPq/sHT2t//N1oHB4cLF4+y7lMRi+BwVfnIJ/lXGaMaAfyeY5oMGNpDLeA1czxOW91E07z69y0nFqWNKQtQrtBWJQhC/huafRCuqCzPjM4A2dnJIIPM5DP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oIqnxF9mCO6WSE81HDTmZmEy173lOcgcSwXsfmZUD5LpVKn1Vq5fmf/z79+YTs3ytve8e7du5Xrd7wftJRKJbmaEK1NNEOQA0U+idiSlE+SGe59cvJJGOQKa7smFo8in3RTkM90Id+2QcmoKPLJI71xKj23B7oRbpEqIO49RZfPM167Isun6m4C+eRfctbqKyWf0bamhVe7QqwEDgIpnwsXTI7AWaH7axEVJmf5QFVtYgGdTyifxWKx3WxeWLl6ca10ca04PS6tlb7+4Zr3k9BisShX4/OvE8WQtjyzhTqXzhSyn+UcmT6NLJ9ymjRC8lbYgvTjSMUlrilN19TGOV2cRrBiKEs3BflMD2TOlVvm5DtayezyucCwe1+/POhTv0leHVK2PEOStwHWZ4U23hV+viFnAabS4gfwbBzRc11UdUvaUuPljpddInGqWic6omaaaTdCB4FO3ob6MB+oWeG/eEtPGt0Ml968pfIx5yr6XF1dbTWb7Waz02p12u1up93rdHrdrt3r9W3b6duO019dXZWriSHUOJs5+f1IOSeEWkpBfh8yVD65atO6vmZRV0lfc9SbQ1TSeXwuW6hTXaNQXkpS5ZNuCvKZGuRcmbAciqGiHEfKb7pEeylImw8k3NBY9NEVUN6O8Zc6qopuEGaJZHeclNWIoNJf5c8xPcH/Bmd8cpIyVwo0mLFkmuovYrm1nbTu+uV0bslWkstniA/zQzMrKO+4cdBMHWU/ZFxUNLKA7iaUz+Xl5danT63mp4mCtkQF7Tl9e3l5Wa6GlT8KGCUAAEg9CeUzn883T06mCtqmFDSfz0u18M/pRAGjBAAA6SehfH4ZDdkYgqpgxgldjBIAAKQd/HfZAAAAQGwgnwAAAEBsIJ8AAABAbCCfAAAAQGwgnwAAAEBsIJ8AAABAbP4D+trqMd9vTwoAAAAASUVORK5CYII=" alt="" />
在dom4j里面提供了两个方法,用来支持xpath
selectNodes("xpath表达式"),获取多个节点
selectSingleNode("xpath表达式"),获取一个节点
应用简单案例:
1.xml文件
<?xml version="1.0" encoding="UTF-8"?> <class>
<student>
<name>张三</name>
<sid>111111</sid>
</student>
<student id="stu1">
<name>李四</name>
<sid>222222</sid>
</student>
</class>
Java代码
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
public class Dom4jXpath { public static void main(String[] args) throws Exception {
select();//查询所有name的值
//select2();//查询id=stu1的学生的name的值
}
//查询所有name的值
private static void select() throws Exception {
SAXReader saxReader=new SAXReader();
Document document=saxReader.read("src/1.xml");
List<Node>list=document.selectNodes("//name");
for(int i=0;i<list.size();i++)
System.out.println(list.get(i).getText());
}
//查询id=stu1的学生的name的值
private static void select2() throws Exception {
SAXReader saxReader=new SAXReader();
Document document=saxReader.read("src/1.xml");
Node node=document.selectSingleNode("//student[@id='stu1']/name");
System.out.println(node.getText());
}
}