Xstream Map <-> XML (2)

황제낙엽 2011.04.29 13:54 조회 수 : 826

site_link3 http://1 
import java.util.AbstractMap;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.converters.collections.AbstractCollectionConverter;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
import com.thoughtworks.xstream.io.xml.DomDriver;
import com.thoughtworks.xstream.mapper.Mapper;

public class XmlToMap {

private String xmlData = null;
private String root = null;

/**
*
*/
public XmlToMap(String xml, String root) {

this.xmlData = xml;
this.root = root;

}

@SuppressWarnings("unchecked")
public Object build() throws Exception {

XStream xStream = new XStream(new DomDriver("UTF-8"));
Mapper mapper = xStream.getMapper();
xStream.alias(root, Map.class);
xStream.registerConverter(new MapConverter(mapper));

Map<String, String> xmlMap = (Map<String, String>) xStream.fromXML(xmlData);

return xmlMap;
}

public static class MapConverter extends AbstractCollectionConverter implements Converter {

public MapConverter(Mapper mapper) {
super(mapper);
}

@SuppressWarnings("rawtypes")
public boolean canConvert(Class type) {
return type.equals(java.util.HashMap.class)
|| type.equals(java.util.Hashtable.class)
|| (type.getName().equals("java.util.LinkedHashMap") 
|| type.getName().equals("sun.font.AttributeMap"));
}

@SuppressWarnings("rawtypes")
public void marshal(Object value, HierarchicalStreamWriter writer, MarshallingContext context) {

AbstractMap map = (AbstractMap) value;
for (Object obj : map.entrySet()) {
Entry ety = (Entry) obj;
writer.startNode(ety.getKey().toString());
writer.setValue(ety.getValue().toString());
writer.endNode();
}

}

@SuppressWarnings( { "unchecked", "rawtypes" })
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext arg1) {

Map elements = new HashMap();
for (; reader.hasMoreChildren(); reader.moveUp()) {

reader.moveDown();

if (reader.hasMoreChildren() == true) {

//재귀호출
elements.put(reader.getNodeName(), unmarshal(reader, arg1));

} else {

String tempValue = reader.getValue();
String tempKey = reader.getNodeName();

if (tempValue != null) {
tempValue = tempValue.trim().replaceAll("n", "");
}

Object gettingValue = elements.get(tempKey);
if (gettingValue != null) {

//가장 하위의 요소들이 중복일경우에는 List 처리를 합니다.
//단 , 상위요소가 중복일경우에는 무시합니다. 하나만 처리됩니다.
// 예
//<abc>
// <record>
//  <ele>1</ele>
//  <ele>2</ele>
// </record>
// <record>
//  <ele>3</ele>
//  <ele>4</ele>
// </record>
//</abc>
//위와 같을경우에 <ele> 요소는 배열처리 되지만 <record>는 나머지는 무시되고 하나만 저장됩니다.

//중복된 요소의 List 처리
if (gettingValue instanceof List) {
((List) gettingValue).add(tempValue);
elements.put(tempKey, gettingValue);
} else if (gettingValue instanceof String) {
List tempList = new ArrayList();
tempList.add(gettingValue);
tempList.add(tempValue);
elements.put(tempKey, tempList);
}

} else {
elements.put(reader.getNodeName(), tempValue);
}

}
}

return elements;
}

}

public static void main(String args[]) {

StringBuilder buffer = new StringBuilder();

buffer.append("<res>      ");
buffer.append(" <param>      ");
buffer.append("  <val1>val1</val1>  ");
buffer.append("  <val2>val2</val2>  ");
buffer.append(" </param>      ");
buffer.append("</res>      ");

XmlToMap test = new XmlToMap(buffer.toString(), "res");
try {
Object oo = test.build();
System.out.println(oo);
} catch (Exception e) {
e.printStackTrace();
}
}
}
번호 제목 글쓴이 날짜 조회 수
50 Hello React 황제낙엽 2025.04.04 479
49 Bootstrap Icons 황제낙엽 2024.10.13 611
48 navbars 예제 황제낙엽 2024.07.15 589
47 Chatbot 용 디자인 템플릿 (ChatGPT) file 황제낙엽 2024.01.28 637
46 (Bing) Mustache 의 특징 황제낙엽 2023.12.21 587
45 문자열의 셀렉션 동작 구현 (<p>, <input>) 황제낙엽 2023.10.02 536
44 Bootstrap 5 Gallery Examples & Freebies 황제낙엽 2023.09.26 696
43 Small Business Template (Start Bootstrap) 황제낙엽 2023.09.25 626
42 Card Component 에 background image 출력하기 황제낙엽 2023.09.08 543
41 Bootstrap Gallery 를 상품 전시용으로 활용하기 위한 분석 황제낙엽 2023.09.06 578
40 dom element 에 collapse 적용하기 file 황제낙엽 2023.08.16 562
39 <i> 태그의 아이콘 종류 찾기 (fontawesome, icon) file 황제낙엽 2023.07.09 554
38 Vanilla JS Datepicker 에서 locales 지정 (한국어로 출력) 황제낙엽 2023.06.19 773
37 Feather-icons (svg) 황제낙엽 2023.06.10 532
36 프레임셋처럼 외부페이지를 동적으로 로드하기 (from bing) 황제낙엽 2023.05.24 578
35 Modal 컴포넌트를 스크립트로 감추고 보이기 황제낙엽 2023.05.22 574
34 bootstrap free date-picker 종류 (from bing) 황제낙엽 2023.05.20 566
33 라디오 버튼을 선택 해제하는 샘플 코드 (from bing) 황제낙엽 2023.05.16 558
32 radio group 에서 아이템을 선택한 값 가져오기 황제낙엽 2023.05.15 531
31 부트스트랩(Bootstrap) 테마(Theme), 템플릿(Template) 무료 사이트 file 황제낙엽 2023.04.05 562