首页 > 代码库 > xml中所有元素转化为map

xml中所有元素转化为map

package demo;


import java.io.ByteArrayInputStream;

import java.io.UnsupportedEncodingException;

import java.util.Iterator;

import java.util.Map;

/*

 *dom4j

*/

import org.dom4j.Document;

import org.dom4j.DocumentException;

import org.dom4j.DocumentHelper;

import org.dom4j.Element;

import org.dom4j.io.SAXReader;


import com.google.common.collect.Maps;

/**

 * 

 * @author wangqm1

 * 

 * 程序功能是:实现将xml格式的报文转化成map格式

 * 

 * 1、首先调用setGenerateXML()方方法组装成xml,然后调用dom4jAnalyze()将所有元素转化为map

 *

 */

public class ParseXML {

public static void main(String[] args) {

try {

System.out.println(dom4jAnalyze(setGenerateXML()));

} catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

e.printStackTrace();

System.out.println("不支持此编码格式");

} catch (DocumentException e) {

// TODO Auto-generated catch block

e.printStackTrace();

System.out.println("不能生成Document");

}

}

 static Map<String, String> dom4jAnalyze(String parameter)

throws DocumentException, UnsupportedEncodingException {

Map<String, String> map = Maps.newConcurrentMap();

// 创建SAXReader读取器,专门用于读取xml

Document doc = new SAXReader().read(new ByteArrayInputStream(parameter

.getBytes("GBK")));

Iterator<?> it = doc.getRootElement().elementIterator();

while (it.hasNext()) {

Element el = (Element) it.next();

map.put(el.getName(), el.getText());

Iterator<?> child = el.elementIterator();

while (child.hasNext()) {

Element ele = (Element) child.next();

map.put(ele.getName(), ele.getText());

Iterator<?> eleChild = ele.elementIterator();

while (eleChild.hasNext()) {

Element chel = (Element) eleChild.next();

map.put(chel.getName(), chel.getText());

}

}

}

return map;

}

 private static String setGenerateXML() {

Document doc = setDocument() ;

Element ele = doc.addElement("stream");

Element E3RDPAYNO = ele.addElement("E3RDPAYNO");

E3RDPAYNO.setText("100599");

Element ORDERMODE = ele.addElement("ORDERMODE");

ORDERMODE.setText("01");

Element ORDERDATE = ele.addElement("ORDERDATE");

ORDERDATE.setText("null");

Element ORDERTIME = ele.addElement("ORDERTIME");

ORDERTIME.setText("12:30:02");

Element ORDERNO = ele.addElement("ORDERNO");

ORDERNO.setText("123456");

Element CURRID = ele.addElement("CURRID");

CURRID.setText("01");

Element ORDERAMT = ele.addElement("ORDERAMT");

ORDERAMT.setText("100");

Element MEMO = ele.addElement("MEMO");

MEMO.setText("");

Element NOTIFYMODE = ele.addElement("NOTIFYMODE");

NOTIFYMODE.setText("01");

Element NOTIFYURL = ele.addElement("NOTIFYURL");

NOTIFYURL.setText("http://106.39.35.10/finExchange/cncbpay/back");

Element RISKLEVEL = ele.addElement("RISKLEVEL");

RISKLEVEL.setText("00");

Element SUPPTCARDTYPE = ele.addElement("SUPPTCARDTYPE");

SUPPTCARDTYPE.setText("01");

Element TTL = ele.addElement("TTL");

TTL.setText("0NOTIFYSCOPE");

Element MEMBERID = ele.addElement("MEMBERID");

MEMBERID.setText("Y");

Element NOTIFYSCOPE = ele.addElement("NOTIFYSCOPE");

NOTIFYSCOPE.setText("01");

Element ISSAFEINF = ele.addElement("ISSAFEINF");

ISSAFEINF.setText("Y");

Element REFERER = ele.addElement("REFERER");

REFERER.setText("pay.ucfpay.com");

return doc.asXML().replace("\n", "");

   }

private static Document setDocument() {

Document doc = DocumentHelper.createDocument();

  doc.setXMLEncoding("GBK");

// doc.setXMLEncoding("UTF-8");

return doc;

   }

}


本文出自 “autoComplete” 博客,请务必保留此出处http://7129486.blog.51cto.com/7119486/1579220

xml中所有元素转化为map