2011-07-07 45 views
2

我想在運行時從肥皂消息中檢索所有xpath。從SOAP消息中檢索Xpath

例如,如果我有這樣

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
<soap:Bodyxmlns:ns1="http://xmlns.oracle.com/TestAppln_jws/TestEmail/TestEmail"> 
<ns1:process> 
      <ns1:To></ns1:To> 
      <ns1:Subject></ns1:Subject> 
      <ns1:Body></ns1:Body> 
     </ns1:process> 
    </soap:Body> 
</soap:Envelope> 

SOAP消息然後從該SOAP消息的可能的XPath是

  1. /soap:Envelope/soap:Body/ns1:process/ns1:To
  2. /soap:Envelope/soap:Body/ns1:process/ns1:Subject
  3. /soap:Envelope/soap:Body/ns1:process/ns1:Body

我該如何回顧那些用java?

回答

0

像這樣的東西可以工作:

string[] paths; 
function RecurseThroughRequest(string request, string[] paths, string currentPath) 
{ 
    Nodes[] nodes = getNodesAtPath(request, currentPath); 
    //getNodesAtPath is an assumed function which returns a set of 
    //Node objects representing all the nodes that are children at the current path 

    foreach(Node n in nodes) 
    { 
     if(!n.hasChildren()) 
     { 
      paths.Add(currentPath + "/" + n.Name); 
     } 
     else 
     { 
      RecurseThroughRequest(paths, currentPath + "/" + n.Name); 
     } 

    } 
} 

,然後調用函數是這樣的:

string[] paths = new string[]; 
RecurseThroughRequest(request, paths, "/"); 

當然這是行不通的出大門,但我認爲,理論就在那裏。

2

使用XPath類型與NamespaceContext

Map<String, String> map = new HashMap<String, String>(); 
map.put("foo", "http://xmlns.oracle.com/TestAppln_jws/TestEmail/TestEmail"); 
NamespaceContext context = ...; //TODO: context from map 
XPath xpath = ...; //TODO: create instance from factory 
xpath.setNamespaceContext(context); 

Document doc = ...; //TODO: parse XML 
String toValue = xpath.evaluate("//foo:To", doc); 

雙斜槓使得這種表達在給定節點中http://xmlns.oracle.com/TestAppln_jws/TestEmail/TestEmail第一To元件相匹配。不要緊,我用foo而不是ns1;前綴映射需要匹配XPath表達式中的那個,而不是文檔中的那個。

您可以在Java: using XPath with namespaces and implementing NamespaceContext中找到更多示例。您可以找到更多使用SOAP here的示例。