Converting XML into Java Map<String, Integer>

假装没事ソ 提交于 2019-12-02 07:35:32

You need to register your MapConverter, the class which implements Converter

xstream.registerConverter(new MapEntryConverter());

Hope that helps

Underscore-java library can convert xml to hashmap and vice verse. I am the maintainer of the project. Live example

Code example:

import com.github.underscore.lodash.U;

public class Main {
    public static void main(String[] args) {
      String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"
    + "<root>"
    + "   <Durapipe type=\"int\">1</Durapipe>"
    + "   <EXPLAIN type=\"int\">2</EXPLAIN>"
    + "   <woods type=\"int\">2</woods>"
    + "   <hanging type=\"int\">3</hanging>"
    + "   <hastily type=\"int\">2</hastily>"
    + "   <localized type=\"int\">1</localized>"
    + "   <Schuster type=\"int\">5</Schuster>"
    + "   <regularize type=\"int\">1</regularize>"
    + "   <LASR type=\"int\">1</LASR>"
    + "   <LAST type=\"int\">22</LAST>"
    + "   <Gelch type=\"int\">2</Gelch>"
    + "   <Gelco type=\"int\">26</Gelco>"
    + "</root>";

    String result = U.fromXmlWithoutAttributes(xml).toString();
    // {Durapipe=1, EXPLAIN=2, woods=2, hanging=3, hastily=2, localized=1, Schuster=5, regularize=1, LASR=1, LAST=22, Gelch=2, Gelco=26}

    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!