Java dom4j selectSingleNode返回空值
XML文件:Java dom4j selectSingleNode返回空值,java,xml,dom4j,Java,Xml,Dom4j,XML文件: <?xml version="1.0" encoding="UTF-8"?> <INVOICE xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fo="http://www.w3.org/1999/XSL/Format"> <HEADER> ... 他们更改了xml文件,如下所示: <?xml version="1.0" encoding="UTF-8
<?xml version="1.0" encoding="UTF-8"?>
<INVOICE xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:fo="http://www.w3.org/1999/XSL/Format">
<HEADER>
...
他们更改了xml文件,如下所示:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Invoice xmlns="http://com.spx/commerce/rasp/transform">
<Header>
...
但它不工作,它返回null。
我应该如何更改代码
提前谢谢
干杯
更新
所以这是一个关于名称空间的问题。
我正在尝试这样的事情
Namespace ns = new Namespace("","http://com.spx/commerce/rasp/transform");
document.add(ns);
document.selectSingleNode("//Invoice/Header");
或
不仅节点的本地名称发生了更改,名称空间也发生了更改。您需要将前缀绑定到命名空间,然后选择使用该前缀
Node node = document.selectSingleNode("//Invoice/Header");
Namespace ns = new Namespace("","http://com.spx/commerce/rasp/transform");
document.add(ns);
document.selectSingleNode("//Invoice/Header");
XPath xpath = DocumentHelper.createXPath( "//Invoice/Header");
Map<String, String> map = new HashMap<String, String>();
map.put("", "http://com.spx/commerce/rasp/transform");
xpath.setNamespaceURIs(map);
Element tagElement = ((Element)xpath.selectSingleNode(document));
HashMap map = new HashMap();
map.put("t", "http://com.spx/commerce/rasp/transform");
XPath xpath = DocumentHelper.createXPath( "//t:Invoice/t:Header");
xpath.setNamespaceURIs(map);
Element tagElement = ((Element)xpath.selectSingleNode(document));