在使用cxf的 wsdl2java 自動生成接口調用類,訪問其中一個接口時,會發現 any 對象根本沒有值。
java
緣由很簡單,由於any被包裝成了一個Element對象,直接輸出或者取值是沒法取到的,node
使用 xerces 包中的 apache
org.apache.xerces.dom.ElementNSImpldom
對象進行接收和解析就能取出動態xml中的全部元素。maven
代碼以下:spa
List<ElementNSImpl> any = reportResult.getCurrentPageData().get(i).getAny(); for (ElementNSImpl m : any) { List<TheTable> tbls = getTables(m); }
private List<TheTable> getTables(ElementNSImpl element){ NodeList node = element.getChildNodes(); List<TheTable> theTables = new ArrayList<TheTable>(); TheTable table = new TheTable(); boolean isTable = false; for (int k = 0; k < node.getLength(); k++) { Object o = node.item(k); ElementNSImpl elementNSImpl = (ElementNSImpl) o; if(elementNSImpl.getChildNodes().getLength()>0){ String nodeName = elementNSImpl.getNodeName(); if("NewDataSet".equals(nodeName)){//若是是NewDataSet,直接返回遞歸結果 return getTables(elementNSImpl); }else if("Table".equals(nodeName)){//若是是Table,把返回結果加入到 theTables 中 theTables.addAll(getTables(elementNSImpl)); }else{ isTable = true; if("AccountNumber".equals(elementNSImpl.getNodeName())){ table.setAccountNumber(elementNSImpl.getTextContent()); }else{ isTable = false; } } } } if (isTable) { theTables.add(table); } return theTables; }
xerces 相關jar包,使用maven或者谷歌百度去搜索吧。code
另一篇給了很大幫助,也是一樣問題的博客地址:http://peterwei.iteye.com/blog/1002311xml