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?