0

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?

WellieeGee
  • 312
  • 1
  • 4
  • 13

1 Answers1

0

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...

WellieeGee
  • 312
  • 1
  • 4
  • 13