Java 查询JAXP XPath无法在WebSphere中的第二次调用中找到节点(JUnit工作正常)
我正在用xml元素检查一个元素,如果不存在,将默认值 这来自WebSphere7上对JAXWS的web服务调用,即org.apache.xerces.dom.ElementNSImplJava 查询JAXP XPath无法在WebSphere中的第二次调用中找到节点(JUnit工作正常),java,xml,dom,xpath,jaxp,Java,Xml,Dom,Xpath,Jaxp,我正在用xml元素检查一个元素,如果不存在,将默认值 这来自WebSphere7上对JAXWS的web服务调用,即org.apache.xerces.dom.ElementNSImpl // instantiate xpath XPathFactory xPathFactory = XPathFactory.newInstance(); XPath xPath = xPathFactory.newXPath(); xPath.setNamespaceContext(new NamespaceCo
// 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);
}
//实例化xpath
XPathFactory XPathFactory=XPathFactory.newInstance();
XPath=xPathFactory.newXPath();
setNamespaceContext(新的NamespaceContext(){
公共字符串getNamespaceURI(字符串前缀){
如果(“ns.”等于(前缀))
返回提供者名称空间;
其他的
返回XMLConstants.NULL\u NS\u URI;
}
公共字符串getPrefix(字符串uri){
返回null;//不适用
}
公共迭代器getPrefixes(字符串uri){
返回null;//不适用
}
});
//检查是否填充了日期
XPathExpression declarationDateXpath=xPath.compile(“//ns:Provider/ns:DeclarationDate”);
Node dateNode=(Node)providerDateXpath.evaluate(Node,XPathConstants.Node);
if(dateNode==null){
//如果没有,请添加节点
Document doc=node.getOwnerDocument();
dateNode=doc.createElements(提供者名称空间,“DeclarationDate”);
XPathExpression providerXPath=xPath.compile(“//ns:Provider”);
节点providerNode=(节点)providerXPath.evaluate(节点,XPathConstants.Node);
providerNode.appendChild(日期节点);
}
//检查值并在必要时设置默认值
if(dateNode.getTextContent()==null | |“”.equals(dateNode.getTextContent()){
//未设置日期,默认为今天
dateNode.setTextContent(今天);
}
正如你所看到的,我正在尽可能多地实例化每一个调用
在第一个web服务调用中,它返回节点。第二个web服务调用,它为两个XPath返回null
根据“XPath[和XPathExpression][对象]不是线程安全的,也不可重入
有什么想法吗?好吧。我已经想好了,我已经成功了
是xpath。准确地说,第二轮是xpath
我将xpath从“//ns:Provider/ns:DeclarationDate”
(其中ns:Provider
是根)缩短为“//ns:DeclarationDate”
JAXP的WebSphere7实现中的某个地方可能存在导致这种情况的缺陷,但不可能/不值得进一步研究
我希望这对将来的人有所帮助