how to parse xml to hashmap?

后端 未结 3 850
北荒
北荒 2021-01-12 20:17

I have an example of an xml I want to parse

 

a
3条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-12 20:44

    There is underscore-java library. Or it's java 8 port. I am the maintainer of the project. Live example

    import com.github.underscore.lodash.U;
    import java.util.Map;
    
    public class Main {
    
      @SuppressWarnings("unchecked")
      public static void main(String[] args) {
    
        Map map = U.fromXmlMap(
            "
    \r\n" + " \r\n" + "\r\n" + " attribute 1 of detail a \r\n" + " attribute 2 of detail a \r\n" + " attribute 3 of detail a \r\n" + "\r\n" + " \r\n" + "\r\n" + " \r\n" + " attribute 1 of detail b \r\n" + " attribute 2 of detail b \r\n" + "\r\n" + " \r\n" + "\r\n" + "\r\n" + "
    "); System.out.println(map); // {Details={detail-a={detail=[ attribute 1 of detail a , attribute 2 of detail a , attribute 3 of detail a ]}, // detail-b={detail=[ attribute 1 of detail b , attribute 2 of detail b ]}}, #omit-xml-declaration=yes} } }

提交回复
热议问题