xml文件根元素中如果有命名空间的定义时,使用dom4j解析xml有问题,去掉命名空间的定义,就不会出问题.解决办法见详细内容说明......希望对有同样问题的朋友有所帮助.
xml文件例子
<?xml version="1.0" encoding="UTF-8"?>
<OnlineTran xmlns="http://www.bocsoft.com/OnlineTran" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.bocsoft.com/OnlineTran
OnlineTranSchema.xsd">
<Trade>
<TranCode>BA050004</TranCode>
<TranDescription>合同账户列表查询</TranDescription>
<Input>
<InputTranData>
<Length>16</Length>
<Type>numeric</Type>
<Option>M</Option>
<Description>合同号码</Description>
</InputTranData>
<InputTranData>
<Length>1</Length>
<Type>alphanumeric</Type>
<Option>M</Option>
<Description>功能码</Description>
</InputTranData>
</Input>
</Trade>
</OnlineTran>
如果其中含有命名空间定义的,解析xml代码如下:
HashMap xmlMap = new HashMap();
String defaultNS = "http://www.bocsoft.com/OnlineTran";
xmlMap.put("x", defaultNS);
XPath x = document.createXPath("//x:OnlineTran/x:Trade/x:Input/x:InputTranData");
x.setNamespaceURIs(xmlMap);
List lt = x.selectNodes(document);