java-将XML转换为嵌套地图的地图
作者:互联网
我有一个带有嵌套元素和重复标签的XML.例如:
<person>
<name>Rama</name>
<age>27</age>
<gender>male</gender>
<address>
<doornumber>234</doornumber>
<street>Kanon</street>
<city>Hyderabad</city>
</address>
<qualification>
<degree>M.Sc</degree>
<specialisation>Maths</specialisation>
</qualification>
<qualification>
<degree>B.E.</degree>
<specialisation>Electrical</specialisation>
</qualification>
</person>
现在,我需要一个API来将XML转换为Java Maps:
{name="Rama",age="27",gender="male",address={doornumber=234,street="Kanon",city="Hyderabad"},qualification=[{degree="M.Sc",specialisation="Maths"},{degree="B.E.",specialisation="Electrical"}]}
我知道我们可以使用XStream API来实现这一点.在这里,我只是想知道使用XStream是否有缺点,是否还有其他更好的Java API可以实现这一点.有什么建议么?
注意:这应该以一般方式完成,即Java API应该适用于任何XML,而不仅适用于上述XML.
解决方法:
不过已经很晚了.但是我已经为XStream API编写了一个自定义MapEntryConverter,它可以处理XML数据的任何复杂性,无论其深度如何.即使它将支持Duplicate标签名(将存储为ArrayList).
XStream xStream = new XStream(new DomDriver());
xStream.registerConverter(new MapEntryConverter());
xStream.alias("xml", java.util.Map.class);
// from XML, convert back to map
Map<String, List<Object>> map = (Map<String, List<Object>>) xStream.fromXML(xmlData);
/*System.out.println("MAP: \n" + map.entrySet().toString());*/
String xml = xStream.toXML(map);
/*System.out.println("XML: \n"+xml);*/
MapEntryConverter.java
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.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
public class MapEntryConverter implements Converter{
@SuppressWarnings("rawtypes")
public boolean canConvert(Class clazz) {
return AbstractMap.class.isAssignableFrom(clazz);
}
@SuppressWarnings({ "unchecked" })
public void marshal(Object value, HierarchicalStreamWriter writer, MarshallingContext context) {
AbstractMap<String, List<?>> map = (AbstractMap<String, List<?>>) value;
List<Map<String, ?>> list = (List<Map<String, ?>>) map.get("xml");
for( Map<String, ?> maps: list ) {
for( Entry<String, ?> entry: maps.entrySet() ) {
mapToXML(writer, entry);
}
}
}
@SuppressWarnings("unchecked")
private void mapToXML(HierarchicalStreamWriter writer, Entry<String, ?> entry) {
writer.startNode(entry.getKey());
if( entry.getValue() instanceof String ) {
writer.setValue(entry.getValue().toString());
}else if( entry.getValue() instanceof ArrayList ) {
List<?> list = (List<?>) entry.getValue();
for( Object object: list ) {
Map<String, ?> map = (Map<String, ?>) object;
for( Entry<String, ?> entryS: map.entrySet() ) {
mapToXML(writer, entryS);
}
}
}
writer.endNode();
}
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
Map<String, List<Object>> map = new HashMap<String, List<Object>>();
map = xmlToMap(reader, new HashMap<String, List<Object>>());
return map;
}
private Map<String, List<Object>> xmlToMap(HierarchicalStreamReader reader, Map<String, List<Object>> map) {
List<Object> list = new ArrayList<Object>();
while(reader.hasMoreChildren()) {
reader.moveDown();
if( reader.hasMoreChildren() ) {
list.add(xmlToMap(reader, new HashMap<String, List<Object>>()));
}else {
Map<String, Object> mapN = new HashMap<String, Object>();
mapN.put(reader.getNodeName(), reader.getValue());
list.add(mapN);
}
reader.moveUp();
}
map.put(reader.getNodeName(), list);
return map;
}
}
请注意,Map中的每个值(如果有任何子级)都存储为列表.根据我的说法,解组部分非常整洁.我将在空闲时间完善并简化编组部分.
输入数据应在xml标记中提供,如下所示.
<xml>
<person>
<name>Rama</name>
<age>27</age>
<gender>male</gender>
<address>
<doornumber>234</doornumber>
<street>Kanon</street>
<city>Hyderabad</city>
</address>
<qualification>
<degree>M.Sc</degree>
<specialisation>Maths</specialisation>
</qualification>
<qualification>
<degree>B.E.</degree>
<specialisation>Electrical</specialisation>
</qualification>
</person>
</xml>
将我的输出与您的预期输出进行比较时,会有一点变化.重复的键值不会合并为数组,而是它们是单独的列表.
标签:xstream,jaxb,xml,spring,java 来源: https://codeday.me/bug/20191122/2056380.html