public static void main(String[] args) { String xml = "<?xml version=\"1.0\" encoding=\"gb2312\"?><p><seqid></seqid><code></code><msg>成功</msg>" + "<node><ename>huoniu</ename><cname>火牛</cname><prefix>108</prefix><begin>20150603</begin><end>20160630</end><borr>1000000</borr><margin>100000</margin><usdborr>1000000</usdborr><usdmargin>100000</usdmargin><mainp>0.60</mainp><midp>0.60</midp><growp>0.60</growp><mainpt>0.60</mainpt><midpt>0.60</midpt><growpt>0.30</growpt><shcomm>0.000300</shcomm><szcomm>0.000300</szcomm><warn>1.000</warn><close>1.000</close><interest>200</interest><commf>13</commf><layout>p-huo01:1100000:574810</layout><unmoney>0</unmoney><tstatus>0</tstatus><cstatus>0</cstatus></node>" + " <node><ename>nn</ename><cname>牛牛</cname><prefix>102</prefix><begin>20150615</begin><end>20151015</end><borr>10000000</borr><margin>8000000</margin><usdborr>10000000</usdborr><usdmargin>8000000</usdmargin><mainp>0.60</mainp><midp>0.60</midp><growp>0.30</growp><mainpt>0.60</mainpt><midpt>0.60</midpt><growpt>0.30</growpt><shcomm>0.003000</shcomm><szcomm>0.003000</szcomm><warn>0.800</warn><close>0.800</close><interest>0</interest><commf>0</commf><layout></layout><unmoney>18000000</unmoney><tstatus>0</tstatus><cstatus>0</cstatus></node>" + "<node><ename>ag-huo</ename><cname>兜底代理商</cname><prefix>huo</prefix><begin>20150602</begin><end>20160630</end><borr>0</borr><margin>0</margin><usdborr>0</usdborr><usdmargin>0</usdmargin><mainp>0.60</mainp><midp>0.60</midp><growp>0.30</growp><mainpt>0.60</mainpt><midpt>0.60</midpt><growpt>0.30</growpt><shcomm>0.000300</shcomm><szcomm>0.000300</szcomm><warn>0.010</warn><close>0.010</close><interest>0</interest><commf>0</commf><layout></layout><unmoney>0</unmoney><tstatus>0</tstatus><cstatus>0</cstatus></node></p>"; List<Map> nodeList = new ArrayList<Map>(); Map<String, Object> sendMap = new HashMap<String, Object>(); try { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(new InputSource(new StringReader(xml))); Element root = doc.getDocumentElement();// 根节点
Node node = root.getFirstChild(); while(node != null) { String nodeName = node.getNodeName().trim(); String nodeValue = node.getTextContent().trim(); if("node".equals(nodeName) && node.hasChildNodes()) { Map<String, Object> nodeMap = new HashMap<String, Object>(); Node childNode = node.getFirstChild(); while(childNode != null) { nodeMap.put(childNode.getNodeName(), childNode.getTextContent()); childNode = childNode.getNextSibling(); } nodeList.add(nodeMap); } else { sendMap.put(nodeName, nodeValue); } node = node.getNextSibling(); } } catch (Exception e) { e.printStackTrace(); } sendMap.put("node", nodeList); System.out.println(sendMap); }打印结果 :
{node=[{warn=1.000, cstatus=0, tstatus=0, borr=1000000, growpt=0.30, unmoney=0, midpt=0.60, usdmargin=100000, commf=13, close=1.000, begin=20150603, shcomm=0.000300, usdborr=1000000, growp=0.60, interest=200, layout=p-huo01:1100000:574810, prefix=108, ename=huoniu, midp=0.60, mainpt=0.60, margin=100000, szcomm=0.000300, cname=火牛, end=20160630, mainp=0.60},
{warn=0.800, cstatus=0, tstatus=0, borr=10000000, growpt=0.30, unmoney=18000000, midpt=0.60, usdmargin=8000000, commf=0, close=0.800, begin=20150615, shcomm=0.003000, usdborr=10000000, growp=0.30, interest=0, layout=, ename=nn, midp=0.60, mainpt=0.60, margin=8000000, szcomm=0.003000, end=20151015, mainp=0.60},
{warn=0.010, cstatus=0, tstatus=0, borr=0, growpt=0.30, unmoney=0, midpt=0.60, usdmargin=0, commf=0, close=0.010, begin=20150602, shcomm=0.000300, usdborr=0, growp=0.30, interest=0, layout=, prefix=huo, ename=ag-huo, midp=0.60, mainpt=0.60, margin=0, szcomm=0.000300, end=20160630, mainp=0.60}], seqid=, code=, msg=成功}
posted on 2015-08-09 10:15
藤本蔷薇 阅读(261)
评论(0) 编辑 收藏