遇到一个问题,与大家分享一下,也想大家帮忙解决一下,Thanks!<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SOAP-ENV:Body>
                <!--  这里有一个m 的命名空间 -->
<m:mt_PS_WBSEstBud xmlns:m="http://www.jsepc.com.cn/sgerp/v1.0">
<IDOCNO>String</IDOCNO>
<ZHTEMP1>String</ZHTEMP1>
<ZHTEMP2>String</ZHTEMP2>
<ITEM>
<projCode>String</projCode>
<singleProjCode>String</singleProjCode>
</ITEM>
</m:mt_PS_WBSEstBud>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
java代码package cnn.util.xml;import java.io.File;import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;public class Dom4jReadXml { public static void main(String[] args) {
readXml();
} public static void readXml() {
String file = "D:\\client.xml";
SAXReader read = new SAXReader();
try {
Document doc = read.read(new File(file)); Element root = doc.getRootElement(); // 这里是获取 元素 projCode, 结果 node.getPath()="/SOAP-ENV:Envelope/SOAP-ENV:Body/m:mt_PS_WBSEstBud/ITEM/projCode"
Node node = root.selectSingleNode("//projCode");

// 如果这里是把 projCode 的路径改写如下,就会报异常
Node node1 = root.selectSingleNode("//SOAP-ENV:Envelope/SOAP-ENV:Body/m:mt_PS_WBSEstBud/ITEM/projCode");
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}Exception in thread "main" org.dom4j.XPathException: Exception occurred evaluting XPath: //SOAP-ENV:Envelope/SOAP-ENV:Body/m:mt_PS_WBSEstBud/ITEM/projCode. Exception: XPath expression uses unbound namespace prefix m------------------------这是第一种情况,如果把xml文件修改一下,就是ok 的<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"  xmlns:m="http://www.jsepc.com.cn/sgerp/v1.0">
<SOAP-ENV:Body>
                <!--  注意这里的m 命名空间放到上面去了,java 代码不变,一切ok -->
<m:mt_PS_WBSEstBud>
<IDOCNO>String</IDOCNO>
<ZHTEMP1>String</ZHTEMP1>
<ZHTEMP2>String</ZHTEMP2>
<ITEM>
<projCode>String</projCode>
<singleProjCode>String</singleProjCode>
</ITEM>
</m:mt_PS_WBSEstBud>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>Problem is here , why ?    Thanks!