title: Java - Convert JSON to XML keeping the attributes
body: <p>Using the org.json json library, it's easy to convert from XML to JSON. but the conversion back to XML always convert JSON attributes into XML nodes:</p>
  
  <pre><code>import org.json.JSONObject;
  import org.json.XML;
  
  public class Test {
      public static void main(String[] args) throws Exception {
          String xml = "&lt;tag1 attr1=\"val1\"&gt;&lt;tag2 attr2=\"val2\"/&gt;&lt;/tag1&gt;";
          System.out.println(xml);
  
          JSONObject str = XML.toJSONObject(xml);
          System.out.println(str);
  
          JSONObject json = new JSONObject(str.toString());
          String xml2 = XML.toString(json);
          System.out.println(xml2);
      }
  }
  </code></pre>
  
  <p>Output</p>
  
  <pre><code>&lt;tag1 attr1="val1"&gt;&lt;tag2 attr2="val2"/&gt;&lt;/tag1&gt;
  {"tag1":{"attr1":"val1","tag2":{"attr2":"val2"}}}
  &lt;tag1&gt;&lt;attr1&gt;val1&lt;/attr1&gt;&lt;tag2&gt;&lt;attr2&gt;val2&lt;/attr2&gt;&lt;/tag2&gt;&lt;/tag1&gt;
  </code></pre>
  
  <p>How can I retrieve my XML attributes?</p>