簡體   English   中英

將XML轉換為嵌套地圖的Map

[英]Convert XML to Map of nested maps

我有一個帶有嵌套元素和重復標記的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中的Map地圖:

{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數據,無論它有多深。 即使它支持重復標記名稱(將存儲為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>

將輸出與預期輸出進行比較時會有一點變化。 重復的鍵值不會合並為數組,而是它們是單獨的列表。

我使用https://github.com/douglascrockford/JSON-java這個項目一次將xml轉換為json。 它很快並且完成了它的工作。

就個人而言,我會使用標准的JAXP接口 - 如果你真的想要一張地圖的地圖,那就是SAX,如果你想保留順序的DOM(一棵樹而不是無序的嵌套地圖),並且想要更好地映射到XML信息集的API。

我認為Melih可能指的是這個解決方案。 這是使用json.org庫的一些Kotlin代碼:

implementation "org.json:json:20180130"build.gradle

import org.json.JSONML

fun xmlToMap(xml: String): MutableMap<String, Any>? {
    return JSONML.toJSONObject(xml).toMap()
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM