场景:将一张表一条记录数据组装成标准XML,将标准XML解析放入HashMap中
1.组装XML
/** 组装XML */
public static String formatDataToXml(Map mapdData) {
StringBuffer xml = new StringBuffer();
xml.append("");
xml.append("");
xml.append("");
if (mapdData != null) {
Set> set = mapdData.entrySet();
Iterator> iterator = set.iterator();
while (iterator.hasNext()) {
Map.Entry entry = (Map.Entry) iterator.next();
xml.append(""
+ entry.getValue() + " ");
}
}
xml.append("
");
xml.append(" ");
return xml.toString();
}
2.解析XML
/** 解析XML */
public static Map parseXml(String xmlData,String attrName) {
logger.info("入参xml:" + xmlData);
StringReader reader= new StringReader(xmlData);
InputSource source = new InputSource(reader);
SAXBuilder saxbuilder = new SAXBuilder();
Map map = new HashMap();
try {
Document doc = saxbuilder.build(source);
Element root = doc.getRootElement();
List children = root.getChildren();
for (Element e : children) {
List row = e.getChildren();
for (Element r : row) {
map.put(r.getAttributeValue(attrName),
r.getText() == null ? "" : r.getText().trim());
}
}
} catch (JDOMException | IOException e) {
e.printStackTrace();
}
return map;
}
3.测试方法
/**测试*/
public static void main(String []args){
logger.info("测试开始.....");
Map mapInfo = new HashMap();
mapInfo.put("CITY_NAME", "厦门");
mapInfo.put("LAND_AREA", "1699.39");
mapInfo.put("POPULATION", "401");
mapInfo.put("GROSS", "4351.18");
mapInfo.put("AREA_NUMBER", "350200");
mapInfo.put("POSTAL_CODE", "361000");
mapInfo.put("TELEPHONE_CODE", "0592");
mapInfo.put("CAR_CODE", "闽D");
mapInfo.put("CITY_DESCRIBE", "适合居住的城市.");
String rtn = formatDataToXml(mapInfo);
Map info = parseXml(rtn,"N");
logger.info("解析信息:" + info.toString());
logger.info(rtn);
logger.info("测试结束.....");
}
4.建表语句
CREATE TABLE `t_city` (
`CITY_NAME` VARCHAR(64) COLLATE utf8_bin NOT NULL COMMENT '城市名',
`LAND_AREA` DOUBLE DEFAULT NULL COMMENT '城市面积',
`POPULATION` BIGINT(16) DEFAULT NULL COMMENT '城市人口',
`GROSS` DOUBLE DEFAULT NULL COMMENT '生产总值',
`AREA_NUMBER` VARCHAR(64) COLLATE utf8_bin DEFAULT NULL COMMENT '行政区划代码',
`POSTAL_CODE` VARCHAR(64) COLLATE utf8_bin DEFAULT NULL COMMENT '邮政编码',
`TELEPHONE_CODE` VARCHAR(64) COLLATE utf8_bin DEFAULT NULL COMMENT '电话区号',
`CAR_CODE` VARCHAR(64) COLLATE utf8_bin DEFAULT NULL COMMENT '车牌代码',
`CITY_DESCRIBE` VARCHAR(512) COLLATE utf8_bin DEFAULT NULL COMMENT '城市描述'
) ENGINE=INNODB DEFAULT CHARSET=utf8 COLLATE=utf8_bin COMMENT='城市信息表'
5.数据模板
闽D
350200
1699.39
0592
厦门
适合居住的城市.
361000
401
4351.18
6.jar包版本
jdom-2.0.6.jar
下载地址: http://www.jdom.org/downloads/index.html
7.完整类
public class OperateXmlUtils {
private static Logger logger = LoggerFactory.getLogger(OperateXmlUtils.class);
/** 组装XML */
public static String formatDataToXml(Map mapdData) {
StringBuffer xml = new StringBuffer();
xml.append("");
xml.append("");
xml.append("");
if (mapdData != null) {
Set> set = mapdData.entrySet();
Iterator> iterator = set.iterator();
while (iterator.hasNext()) {
Map.Entry entry = (Map.Entry) iterator.next();
xml.append(""
+ entry.getValue() + " ");
}
}
xml.append("
");
xml.append(" ");
return xml.toString();
}
/** 解析XML */
public static Map parseXml(String xmlData,String attrName) {
logger.info("入参xml:" + xmlData);
StringReader reader= new StringReader(xmlData);
InputSource source = new InputSource(reader);
SAXBuilder saxbuilder = new SAXBuilder();
Map map = new HashMap();
try {
Document doc = saxbuilder.build(source);
Element root = doc.getRootElement();
List children = root.getChildren();
for (Element e : children) {
List row = e.getChildren();
for (Element r : row) {
map.put(r.getAttributeValue(attrName),
r.getText() == null ? "" : r.getText().trim());
}
}
} catch (JDOMException | IOException e) {
e.printStackTrace();
}
return map;
}
/**测试*/
public static void main(String []args){
logger.info("测试开始.....");
Map mapInfo = new HashMap();
mapInfo.put("CITY_NAME", "厦门");
mapInfo.put("LAND_AREA", "1699.39");
mapInfo.put("POPULATION", "401");
mapInfo.put("GROSS", "4351.18");
mapInfo.put("AREA_NUMBER", "350200");
mapInfo.put("POSTAL_CODE", "361000");
mapInfo.put("TELEPHONE_CODE", "0592");
mapInfo.put("CAR_CODE", "闽D");
mapInfo.put("CITY_DESCRIBE", "适合居住的城市.");
String rtn = formatDataToXml(mapInfo);
Map info = parseXml(rtn,"N");
logger.info("解析信息:" + info.toString());
logger.info(rtn);
logger.info("测试结束.....");
}
}
以上,TKS