@time 2013-07-24
*/
public class XMLUtil {
private static final String SEPARATOR = " ";
/**
/**
/**
/**
private static void map2Xml(StringBuffer sbf, Map map) {
Set s = map.entrySet();
Iterator i = s.iterator();
StringBuffer childSbf = new StringBuffer();
while(i.hasNext()){
Entry e = (Entry) i.next();
String key = (String) e.getKey();
if(key.indexOf("@") >= 0){
continue;
}
if(key != null){
childSbf.append("
}
String attr = "@" + key;
Map attrMap = (Map) map.get(attr);
if(null != attrMap){
Set attrSet = attrMap.entrySet();
Iterator attrIterator = attrSet.iterator();
while(attrIterator.hasNext()){
Entry attrEntry = (Entry) attrIterator.next();
String attrKey = (String) attrEntry.getKey();
String attrValue = (String) attrEntry.getValue();
childSbf.append(SEPARATOR).append(attrKey).append("=").append("\"").append(attrValue).append("\"");
}
}
childSbf.append(">");
Object value = e.getValue();
if(value instanceof List){
List list = (List) value;
for(int j = 0; j Map valueMap = (Map) list.get(j);
map2Xml(childSbf,valueMap);
}
}else if(value instanceof Map){
Map valueMap = (Map) value;
map2Xml(childSbf, valueMap);
}else{
if(null != value){
childSbf.append(value);
}else{
childSbf.append("");
}
}
if(key != null){
childSbf.append("").append(key).append(">");
}
}
sbf.append(childSbf);
}
public static void main(String[] args) {
String xmlStr6 = "";
Map map6 = xml2Map(xmlStr6);
System.out.println(map6.toString());String result = map2Xml(map6);
System.out.println(result);
}
}