通过dom4j XPath奇怪行为解析带有命名空间的xml

时间:2012-03-13 10:17:25

标签: java xml xpath

问题是 - 当我通过XPath解析带有命名空间的XML时,它部分解析XML,例如,如果我设置为XPath:/ SOAP-ENV:Envelope / SOAP-ENV:Body - 解析器识别这个好,但是如果我更进一步 - 解析器粉碎而不返回异常。

这是一个XML:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:m0="http://schemas.compassplus.com/two/1.0/fimi.xsd"
xmlns:m="http://schemas.compassplus.com/two/1.0/fimi.wsdl">
<SOAP-ENV:Body>
<m:GetAcctInfoRp xmlns:m="http://schemas.compassplus.com/two/1.0/fimi.xsd">
<Response Response="1" Product="FIMI" Ver="0"><m0:Avail>995526.4</m0:Avail>
<m0:Bonus>0</m0:Bonus>
<m0:Branch>1</m0:Branch>
<m0:Cards><m0:Row>
<m0:PAN>6706250002450356</m0:PAN>
<m0:MBR>0</m0:MBR>
<m0:CardUID>E4BFBC24A2844F13BE5C5AEEB15D27CE</m0:CardUID>
<m0:Status>1</m0:Status>
</m0:Row>
<m0:Row>
<m0:PAN>6706255660781224</m0:PAN>
<m0:MBR>0</m0:MBR>
<m0:CardUID>971111C18D774C3BA26434336CB57475</m0:CardUID>
<m0:Status>1</m0:Status>
</m0:Row>
</m0:Cards>
<m0:CreditHold>0</m0:CreditHold>
<m0:Currency>810</m0:Currency>
<m0:DebitHold>50240.81</m0:DebitHold>
<m0:DropTmpOverOnRefresh>0</m0:DropTmpOverOnRefresh>
<m0:FoundAccount>40817810200001058114</m0:FoundAccount>
<m0:FoundAccountUID>E79459BEEEF94BEFBA57D0D23503EF7E</m0:FoundAccountUID>
<m0:LastDepAmount>10</m0:LastDepAmount>
<m0:LastDepTime>2012-03-06T17:23:35</m0:LastDepTime>
<m0:LastRefreshTime>2012-02-22T12:49:47</m0:LastRefreshTime>
<m0:LastTranId>4172</m0:LastTranId>
<m0:LastWdlAmount>100</m0:LastWdlAmount>
<m0:LastWdlTime>2012-03-06T17:29:44</m0:LastWdlTime>
<m0:Ledger>1045767.21</m0:Ledger>
<m0:MaskBalances>0</m0:MaskBalances>
<m0:Overdraft>9000000</m0:Overdraft>
<m0:PersonExtId>1891</m0:PersonExtId>
<m0:PersonFIO>bla bla bla</m0:PersonFIO>
<m0:PersonId>782</m0:PersonId>
<m0:Remain>1046068.21</m0:Remain>
<m0:Status>3</m0:Status>
<m0:TmpOverdraft>0</m0:TmpOverdraft>
<m0:Type>1</m0:Type>
<m0:UserFields><m0:Row>
<m0:Name>test</m0:Name>
<m0:TextValue>тест</m0:TextValue>
</m0:Row>
</m0:UserFields>
</Response>
</m:GetAcctInfoRp>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

应该解析此XML文件的Java代码:

String res = RequestMethods.executeXMLPostRequest(url, xml);
Document doc = DocumentHelper.parseText(res.trim());

        String session = doc.valueOf("SOAP-ENV:Envelope/SOAP-ENV:Body/m:GetAcctInfoRp/Response/m0:Avail");
PS对不起我的英文,可能是有人解决了这个问题了吗? 请帮忙。

第一个问题得到解决,另一个问题是 - 如何进入循环结构,例如,如果我们有几个SOAP-ENV:Envelope / SOAP-ENV:Body // m0:Avail元素?

1 个答案:

答案 0 :(得分:1)

这对我有用

public static void main(String[] args) throws DocumentException {
    String xmlText = getContents(new File("/home/bpgergo/Temp/9682103.xml"));
    Document doc = DocumentHelper.parseText(xmlText);
    String session = doc.valueOf("SOAP-ENV:Envelope/SOAP-ENV:Body//m0:Avail");
    System.out.println("session:"+session);
}