查询 JAXP XPath 在 WebSphere 中第二次调用时找不到节点(JUnit 工作正常)

发布于 2024-12-02 03:50:38 字数 1717 浏览 4 评论 0原文

我正在检查带有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浊酒尽余欢 2024-12-09 03:50:38

好吧,好吧。 我已经弄清楚了 我已经让它发挥作用了。

这是 xpath。好吧,准确地说,这是第二次的 xpath。

我将 xpath 从 "//ns:Provider/ns:DeclarationDate" (其中 ns:Provider 是根)缩短为 "//ns:DeclarationDate"

JAXP 的 WebSphere 7 实现中的某个缺陷会导致这种情况,但不可能/不值得进​​一步调查。

我希望这对将来的人有帮助......

Well ok. I've figured it out I'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" (where ns: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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文