xml如下
<?xml version="1.0" encoding="UTF-8"?><ResponseInfo><ResCode>Success</ResCode><ResMessage>Success</ResMessage><ErrorNo>0</ErrorNo><ResultToken>1000_httpd_20161107215454_c37de700_48bd696a</ResultToken><Result pageindex="1"><CardNumber>6214867788922101882</CardNumber><UserName></UserName><ValidDate></ValidDate><BankName>招商银行</BankName><BankId>3080000</BankId><CardName>银联IC金卡</CardName><CardType>借记卡</CardType></Result></ResponseInfo>
Java解析xml代码如下
import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; /** * 解析xml 银行卡 * parseXMlToBankCard * @author sunjianbo * @time 2016年10月27日下午2:21:42 * @param protocolXML * @param cardType * @return */ public ResultMsg parseXMlToBankCard(String protocolXML,ResultMsg rsm) { try { DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc; doc = builder .parse(new InputSource(new StringReader(protocolXML))); Element root = doc.getDocumentElement(); NodeList nodes = root.getChildNodes(); BankCard bankCard = new BankCard(); if (nodes != null) { //<ResCode>第一个子节点 ,是否是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(); } return rsm; }
时间: 2024-10-05 04:58:32