Java 从SOAP消息检索Xpath
我想在运行时从soap消息中检索所有XPath 例如,如果我有一条soap消息,如Java 从SOAP消息检索Xpath,java,xml,soap,Java,Xml,Soap,我想在运行时从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>
<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是
/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检索这些内容?类似的方法可以工作:
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, "/");
当然,这在大门外是行不通的,但我认为理论是存在的。使用带有字母的字体
Map Map=newhashmap();
地图。放置(“foo”http://xmlns.oracle.com/TestAppln_jws/TestEmail/TestEmail");
名称空间上下文=//TODO:来自映射的上下文
XPath=//TODO:从工厂创建实例
setNamespaceContext(context);
文件文件=//TODO:解析XML
字符串toValue=xpath.evaluate(“//foo:To”,doc);
双正斜杠使该表达式与http://xmlns.oracle.com/TestAppln_jws/TestEmail/TestEmail
在给定节点中。我使用foo
而不是ns1
并不重要;前缀映射需要与XPath表达式中的匹配,而不是与文档中的匹配
您可以在中找到更多示例。您可以找到使用SOAP的更多示例
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);