【问题标题】:Retrieving Xpath from SOAP Message从 SOAP 消息中检索 Xpath
【发布时间】:2011-07-07 11:09:37
【问题描述】:

我想在运行时从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消息中可能的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 检索那些?

【问题讨论】:

    标签: java xml soap


    【解决方案1】:

    使用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 的示例。

    【讨论】:

      【解决方案2】:

      这样的事情可能会奏效:

      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, "/");
      

      当然,那是不可能的,但我认为理论是存在的。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-05-04
        • 2018-08-25
        • 2011-01-13
        相关资源
        最近更新 更多