importjavax.xml.parsers.DocumentBuilder;importjavax.xml.parsers.DocumentBuilderFactory;importjavax.xml.parsers.ParserConfigurationException;importorg.w3c.dom.Document;importorg.w3c.dom.Element;importorg.w3c.dom.Node;importorg.w3c.dom.NodeList;/*** 解析xml 银行卡
* parseXMlToBankCard
*@authorsunjianbo
* @time 2016年10月27日下午2:21:42
*@paramprotocolXML
*@paramcardType
*@return
*/
publicResultMsg parseXMlToBankCard(String protocolXML,ResultMsg rsm) {try{
DocumentBuilderFactory factory=DocumentBuilderFactory
.newInstance();
DocumentBuilder builder=factory.newDocumentBuilder();
Document doc;
doc=builder
.parse(new InputSource(newStringReader(protocolXML)));
Element root=doc.getDocumentElement();
NodeList nodes=root.getChildNodes();
BankCard bankCard= newBankCard();if (nodes != null) {//第一个子节点 ,是否是Success
Node firstNode = nodes.item(0);
String firstNodeValue=firstNode.getFirstChild().getNodeValue();if(null !=firstNodeValue && "Success".equals(firstNodeValue)){for(int j = 0; j < nodes.getLength(); j++){
Node node=nodes.item(j);if(null != node.getNodeName() && "Result".equals(node.getNodeName())){//解析银行卡
NodeList nls=node.getChildNodes();for(int i= 0; i < nls.getLength(); i++){
Node nl=nls.item(i);if(nl.getNodeName()!= null && nl.getFirstChild() != null){//System.out.println("节点**" + nl.getNodeName() + "\ttext="//+ nl.getFirstChild().getNodeValue());
if(nl.getFirstChild().getNodeValue() != null){if("BankName".equals(nl.getNodeName()) ){
bankCard.setBankName(nl.getFirstChild().getNodeValue());
}/*if("BankId".equals(nl.getNodeName()) ){
bankCard.setCardNo(nl.getFirstChild().getNodeValue());
}*/
if("CardName".equals(nl.getNodeName()) ){
bankCard.setCardName(nl.getFirstChild().getNodeValue());
}if("CardType".equals(nl.getNodeName()) ){
bankCard.setCardType(nl.getFirstChild().getNodeValue());
}if("CardNumber".equals(nl.getNodeName()) ){
bankCard.setCardNo(nl.getFirstChild().getNodeValue());
}
rsm.setRtncode(0);
rsm.setRtninfo(bankCard);
}
}
}
}
}
}else{
Node secendtNode= nodes.item(1);
String secendtNodeValue=secendtNode.getFirstChild().getNodeValue();
rsm.setErrmsg(secendtNodeValue);
}
}else{
rsm.setErrmsg("Test failed, did not get test results, please test again");
}
}catch(SAXException e) {//TODO Auto-generated catch block
e.printStackTrace();
}catch(IOException e) {//TODO Auto-generated catch block
e.printStackTrace();
}catch(ParserConfigurationException e) {//TODO Auto-generated catch block
e.printStackTrace();
}returnrsm;
}