Вопрос

How to convert Map to XML when inside Map there can be another Map in the value field. There are methods for this with using third party library but how to do it recursively?

e.g. My map is :-

 {
     Type=SPEND, 
     Contact=
     {
       Name=ABC BANK
     }, 

     LineItems=
     {
       LineItem=
       { 
          Description=BANK ACCOUNT FEE, 
           AccouceCode=404, 
           UnitAmount=20.00
       }
     }
     BankAccount=
     {
       Code=BANK-ABC
     },
    }

and the XML format after converting is :-

<BankTransaction>
      <Type>SPEND</Type>
      <Contact>
        <Name>ABC Bank</Name>
      </Contact>
      <LineItems>
        <LineItem>
          <Description>Yearly Bank Account Fee</Description>
          <UnitAmount>20.00</UnitAmount>
          <AccountCode>404</AccountCode>
        </LineItem>
      </LineItems>
      <BankAccount>
        <Code>BANK-ABC</Code>
      </BankAccount>
</BankTransaction>

I have written code for that but it is not converting in proper format. Any help would be appreciate.

public static String MaptoXML(Map<String,Object> params, String root)
    {
        StringBuilder sb = new StringBuilder();
        sb.append("<");
        sb.append(root);
        sb.append(">");

        Iterator<String> it = params.keySet().iterator();
        while(it.hasNext())
        {
            String key = (String)it.next();
            Object obj = params.get(key);

            if(obj instanceof Map)
            {
                Map<String, Object> rec = (Map)obj;
                String s ;
                s = MaptoXML(rec,key);
                sb.append(s);
            }

            sb.append("<");
            sb.append(key);
            sb.append(">");

            sb.append(params.get(key));

            sb.append("</");
            sb.append(key);
            sb.append(">"); 
        }
        sb.append("</");
        sb.append(root);
        sb.append(">");

        return sb.toString();

    }
Это было полезно?

Решение

You are doing slightly mistake. This is the correct code

public static String MaptoXML(Map<String,Object> params, String root)
    {
        StringBuilder sb = new StringBuilder();
        sb.append("<");
        sb.append(root);
        sb.append(">");

        Iterator<String> it = params.keySet().iterator();
        while(it.hasNext())
        {
            String key = it.next();
            Object obj = params.get(key);

            if(obj instanceof Map)
            {
                Map<String, Object> rec = (Map)obj;
                String s ;
                s = MaptoXML(rec,key);
                sb.append(s);
            }
            else
            {
                sb.append("<");
                sb.append(key);
                sb.append(">");

                sb.append(params.get(key));

                sb.append("</");
                sb.append(key);
                sb.append(">"); 
            }
        }
        sb.append("</");
        sb.append(root);
        sb.append(">");

        return sb.toString();   
    }

It will work for nested map also.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top