input: Write a python function to convert XML data to JSON format.
  assert xml_to_json('<root><element key="value">text</element></root>') == '{"root": {"element": {"@key": "value", "#text": "text"}}}'
  assert xml_to_json('<root><child1>value1</child1><child2>value2</child2></root>') == '{"root": {"child1": "value1", "child2": "value2"}}'
  assert xml_to_json('<root><element>text</element><element>another</element></root>') == '{"root": {"element": ["text", "another"]}}'
output: def xml_to_json(xml_str):
      import xml.etree.ElementTree as ET
      import json
  
      def parse_element(element):
          parsed_data = {}
          if element.attrib:
              for key, value in element.attrib.items():
                  parsed_data[f"@{key}"] = value
          if element.text and element.text.strip():
              parsed_data["#text"] = element.text.strip()
          for child in element:
              child_data = parse_element(child)
              if child.tag in parsed_data:
                  if not isinstance(parsed_data[child.tag], list):
                      parsed_data[child.tag] = [parsed_data[child.tag]]
                  parsed_data[child.tag].append(child_data[child.tag])
              else:
                  parsed_data[child.tag] = child_data[child.tag]
          return {element.tag: parsed_data}
  
      root = ET.fromstring(xml_str)
      parsed_data = parse_element(root)
      return json.dumps(parsed_data)