我想在运行时检索soap消息中的所有xpath。
例如,如果我有一个soap消息,如
<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:Envelope/soap:Body/ns1:process/ns1:To
-
/soap:Envelope/soap:Body/ns1:process/ns1:Subject
-
/soap:Envelope/soap:Body/ns1:process/ns1:Body
我怎么能检索那些与java?
与NamespaceContext一起使用XPath类型
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中找到进一步的示例:对名称空间使用XPath并实现NamespaceContext。您可以在这里找到使用SOAP的更多示例。
可以这样做:
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, "/");
当然,这不会在大门之外工作,但我认为理论是存在的。