2011-07-07 13 views
2

実行時にSOAPメッセージからすべてのxpathを取得したいとします。SOAPメッセージからXpathを取得する

Iは

<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でそれらを取得できますか?

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); 
     } 

    } 
} 

をしてから、このようなもので関数を呼び出す:

答えて

0

このような何かは仕事ができる

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要素と一致させます。 ns1の代わりにfooを使用しても問題ありません。接頭辞マッピングはXPath式のものと一致する必要があり、文書内のものは一致しません。

さらなる例はJava: using XPath with namespaces and implementing NamespaceContextです。 SOAP hereを使って作業するさらなる例が見つかります。

関連する問題