input: Write a function to convert an XML string to a JSON string.
  assert xml_to_json('<bookstore><book><title>Harry Potter</title><author>J.K. Rowling</author></book><book><title>The Lord of the Rings</title><author>J.R.R. Tolkien</author></book></bookstore>') == '{"bookstore": [{"title": "Harry Potter", "author": "J.K. Rowling"}, {"title": "The Lord of the Rings", "author": "J.R.R. Tolkien"}]}'
  assert xml_to_json('<bookstore><book><title>1984</title><author>George Orwell</author></book><book><title>Animal Farm</title><author>George Orwell</author></book></bookstore>') == '{"bookstore": [{"title": "1984", "author": "George Orwell"}, {"title": "Animal Farm", "author": "George Orwell"}]}'
output: def xml_to_json(xml_str):
      import xml.etree.ElementTree as ET
      import json
      
      def etree_to_dict(t):
          d = {t.tag: {} if t.attrib else None}
          children = list(t)
          if children:
              dd = {}
              for dc in map(etree_to_dict, children):
                  for k, v in dc.items():
                      if k in dd:
                          if isinstance(dd[k], list):
                              dd[k].append(v)
                          else:
                              dd[k] = [dd[k], v]
                      else:
                          dd[k] = v
              d = {t.tag: dd}
          if t.attrib:
              d[t.tag].update(('@' + k, v) for k, v in t.attrib.items())
          if t.text:
              text = t.text.strip()
              if children or t.attrib:
                  if text:
                      d[t.tag]['#text'] = text
              else:
                  d[t.tag] = text
          return d
  
      root = ET.fromstring(xml_str)
      dict_data = etree_to_dict(root)
      return json.dumps(dict_data)