book.xml
<?xml version="1.0" encoding="utf-8"?> <config> <cooperator code="bsg"> <comefrom code="bsg002"> <ip> </ip> <key><![CDATA[Tc7ib084USQ5LCEmaaBUlNDLXSLvWQlcEepFWdcV8Ac44RFShWxNXhBexKkhDn8g]]></key> <synccallback>http://xxx/return.jsp</synccallback> <timeinterval>30</timeinterval> </comefrom> </cooperator> </config>
java
import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.io.SAXReader; public class MyDomParse { public static void main(String args[]) { try { InputStream x=new FileInputStream("d:/book.xml"); org.dom4j.io.SAXReader saxReader = new SAXReader(); Document doc = saxReader.read(x); String xpath ="/config/cooperator/comefrom/key"; System.out.print(doc.selectSingleNode(xpath).getText()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }需要 dom4j.jar,jaxen-1.1.2.jar 原文链接:https://www.f2er.com/xml/299892.html