查询 JAXP XPath 在 WebSphere 中第二次调用时找不到节点(JUnit 工作正常)
我正在检查带有 xml 元素的元素,如果不存在,将默认一个值。
这是来自 Websphere 7 上 JAXWS 的 Web 服务调用,作为 org.apache.xerces.dom.ElementNSImpl。
// instantiate xpath
XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xPath = xPathFactory.newXPath();
xPath.setNamespaceContext(new NamespaceContext() {
public String getNamespaceURI(String prefix) {
if ("ns".equals(prefix))
return PROVIDER_NAMESPACE;
else
return XMLConstants.NULL_NS_URI;
}
public String getPrefix(String uri) {
return null; // n/a
}
public Iterator<?> getPrefixes(String uri) {
return null; // n/a
}
});
// Check if date is populated
XPathExpression declarationDateXpath = xPath.compile("//ns:Provider/ns:DeclarationDate");
Node dateNode = (Node) providerDateXpath.evaluate(node, XPathConstants.NODE);
if (dateNode == null) {
// if not there, add the node
Document doc = node.getOwnerDocument();
dateNode = doc.createElementNS(PROVIDER_NAMESPACE, "DeclarationDate");
XPathExpression providerXPath = xPath.compile("//ns:Provider");
Node providerNode = (Node) providerXPath.evaluate(node, XPathConstants.NODE);
providerNode.appendChild(dateNode);
}
// Check value & set default if necessary
if (dateNode.getTextContent() == null || "".equals(dateNode.getTextContent())) {
// date not set, defaulting to today
dateNode.setTextContent(today);
}
正如您所看到的,我在每次调用时都会尽可能多地实例化所有内容。
第一个 Web 服务调用,它返回节点。第二个 Web 服务调用,它为两个 xpath 返回 null。
根据 javadoc “XPath[和 XPathExpression] [对象] 不是线程安全的并且不可重入。
有什么想法吗?
I'm checking for an element with an xml element, and will be defaulting a value if not present.
This is coming in from a web service call into JAXWS on Websphere 7 as a org.apache.xerces.dom.ElementNSImpl.
// instantiate xpath
XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xPath = xPathFactory.newXPath();
xPath.setNamespaceContext(new NamespaceContext() {
public String getNamespaceURI(String prefix) {
if ("ns".equals(prefix))
return PROVIDER_NAMESPACE;
else
return XMLConstants.NULL_NS_URI;
}
public String getPrefix(String uri) {
return null; // n/a
}
public Iterator<?> getPrefixes(String uri) {
return null; // n/a
}
});
// Check if date is populated
XPathExpression declarationDateXpath = xPath.compile("//ns:Provider/ns:DeclarationDate");
Node dateNode = (Node) providerDateXpath.evaluate(node, XPathConstants.NODE);
if (dateNode == null) {
// if not there, add the node
Document doc = node.getOwnerDocument();
dateNode = doc.createElementNS(PROVIDER_NAMESPACE, "DeclarationDate");
XPathExpression providerXPath = xPath.compile("//ns:Provider");
Node providerNode = (Node) providerXPath.evaluate(node, XPathConstants.NODE);
providerNode.appendChild(dateNode);
}
// Check value & set default if necessary
if (dateNode.getTextContent() == null || "".equals(dateNode.getTextContent())) {
// date not set, defaulting to today
dateNode.setTextContent(today);
}
As you can see I'm instantiating everything as much as I can each call.
The first web service call, it works returning the nodes. The second web service call, it returns null for both xpaths.
According to the javadoc "XPath[and XPathExpression] [objects are] not thread-safe and not reentrant.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,好吧。
我已经弄清楚了我已经让它发挥作用了。这是 xpath。好吧,准确地说,这是第二次的 xpath。
我将 xpath 从
"//ns:Provider/ns:DeclarationDate"
(其中ns:Provider
是根)缩短为"//ns:DeclarationDate"
。JAXP 的 WebSphere 7 实现中的某个缺陷会导致这种情况,但不可能/不值得进一步调查。
我希望这对将来的人有帮助......
Well ok.
I've figured it outI've made it work.It was the xpath. Well, to be precise, it was the xpath the second time round.
I shortened the xpath from
"//ns:Provider/ns:DeclarationDate"
(wherens:Provider
is the root) to"//ns:DeclarationDate"
.There will be a defect somewhere in the WebSphere 7 implementation of JAXP that is causing this, but it's not possible/worthwhile to investigate further.
I hope this helps someone in the future...