title: Save data in XML file in Python
body: <p>I am trying to save my data to an XML file. This data comes from a website where I want to collect the reviews. There are always five reviews per page, which I want to save in XML format in a file. The problem is that if I print out the XML tree with <code>print(ET.tostring(root, encoding='utf8').decode('utf8'))</code> then there are all five reviews that I want to have. But if I save them into the file with <code>tree.write("test.xml", encoding='unicode')</code> then I only see one review... Here is my code: </p>
  
  <pre><code>import requests
  from bs4 import BeautifulSoup
  import re
  import json
  import xml.etree.cElementTree as ET
  
  source = requests.get('https://www.tripadvisor.ch/Hotel_Review-g188113-d228146-Reviews-Coronado_Hotel-Zurich.html#REVIEWS').text
  
  soup = BeautifulSoup(source, 'lxml')
  pattern = re.compile(r'window.__WEB_CONTEXT__={pageManifest:(\{.*\})};')
  script = soup.find("script", text=pattern)
  dictData = pattern.search(script.text).group(1)
  jsonData = json.loads(dictData)
  
  def get_countrycitydata():
  
      countrycity_dict = dict()
  
      country_data = jsonData['urqlCache']['3960485871']['data']['locations']
      for data in country_data:
          data1 = data['parents']
          countrycity_dict["country_name"] = data1[2]['name']
          countrycity_dict["tripadvisorid_city"] = data1[0]['locationId']
          countrycity_dict["city_name"] = data1[0]['name']
  
      return countrycity_dict
  
  def get_hoteldata():
  
      hotel_dict = dict()
  
      locations = jsonData['urqlCache']['669061039']['data']['locations']
      for data in locations:
          hotel_dict["tripadvisorid_hotel"] = data['locationId']
          hotel_dict["hotel_name"] = data['name']
  
      return hotel_dict
  
  def get_reviews():  
  
      all_dictionaries = []
  
      for locations in jsonData['urqlCache']['669061039']['data']['locations']:
          for reviews in locations['reviewListPage']['reviews']:
  
              review_dict = {}
  
              review_dict["reviewid"] = reviews['id']
              review_dict["reviewurl"] =  reviews['absoluteUrl']
              review_dict["reviewlang"] = reviews['language']
              review_dict["reviewtitle"] = reviews['title']
              reviewtext = reviews['text']
              clean_reviewtext = reviewtext.replace('\n', ' ')
              review_dict["reviewtext"] = clean_reviewtext
  
              all_dictionaries.append(review_dict)
  
      return all_dictionaries
  
  def xml_tree(new_dict): # should I change something here???
  
      root = ET.Element("countries")
      country = ET.SubElement(root, "country")
  
      ET.SubElement(country, "name").text = new_dict["country_name"]
      city = ET.SubElement(country, "city")
  
      ET.SubElement(city, "tripadvisorid").text = str(new_dict["tripadvisorid_city"])
      ET.SubElement(city, "name").text = new_dict["city_name"]
      hotels = ET.SubElement(city, "hotels")
  
      hotel = ET.SubElement(hotels, "hotel")
      ET.SubElement(hotel, "tripadvisorid").text = str(new_dict["tripadvisorid_hotel"])
      ET.SubElement(hotel, "name").text = new_dict["hotel_name"]
      reviews = ET.SubElement(hotel, "reviews")
  
      review = ET.SubElement(reviews, "review")
      ET.SubElement(review, "reviewid").text = str(new_dict["reviewid"])
      ET.SubElement(review, "reviewurl").text = new_dict["reviewurl"]
      ET.SubElement(review, "reviewlang").text = new_dict["reviewlang"]
      ET.SubElement(review, "reviewtitle").text = new_dict["reviewtitle"]
      ET.SubElement(review, "reviewtext").text = new_dict["reviewtext"]
  
      tree = ET.ElementTree(root)
      tree.write("test.xml", encoding='unicode')  
  
      print(ET.tostring(root, encoding='utf8').decode('utf8'))
  
  ##########################################################  
  
  def main():
  
      city_dict = get_countrycitydata()
      hotel_dict = get_hoteldata()
      review_list = get_reviews()
  
      for index in range(len(review_list)):
          new_dict = {**city_dict, **hotel_dict, **review_list[index]}
  
          xml_tree(new_dict)
  
  if __name__ == "__main__":
      main()  
  </code></pre>
  
  <p>How can I change the XML tree so that all five reviews are saved in the file? The XML file should look like this: </p>
  
  <pre><code>&lt;countries&gt;
      &lt;country&gt;
          &lt;name&gt;Schweiz&lt;/name&gt;
          &lt;city&gt;
              &lt;tripadvisorid&gt;188113&lt;/tripadvisorid&gt;
              &lt;name&gt;Zürich&lt;/name&gt;
              &lt;hotels&gt;
                  &lt;hotel&gt;
                      &lt;tripadvisorid&gt;228146&lt;/tripadvisorid&gt;
                      &lt;name&gt;Hotel Coronado&lt;/name&gt;
                      &lt;reviews&gt;
                          &lt;review&gt;
                              &lt;reviewid&gt;672052111&lt;/reviewid&gt; 
                              &lt;reviewurl&gt;https://www.tripadvisor.ch/ShowUserReviews-g188113-d228146-r672052111-Coronado Hotel-Zurich.html&lt;/reviewurl&gt;
                              &lt;reviewlang&gt;de&lt;/reviewlang&gt;
                              &lt;reviewtitle&gt;Optimale Lage und Preis&lt;/reviewtitle&gt;
                              &lt;reviewtext&gt;Hervorragendes Hotel.Beste Erfahrun mit Service und Zimme.Die Qalität der Betten ist optimalr. Zimmer sind trotz geringer Größe sehr gut ausgestattet.Der Föhn war in diesem Fall (nicht in früheren)etwas lahm&lt;/reviewtext&gt;
                          &lt;/review&gt;
                          &lt;review&gt;
                           second review here ...
                          &lt;/review&gt;
                          &lt;review&gt;
                           third review here ...
                          &lt;/review&gt;
                          ...
                      &lt;/reviews&gt;
                  &lt;/hotel&gt;
              &lt;/hotels&gt;
          &lt;/city&gt;
      &lt;/country&gt;
  &lt;/countries&gt;
  
  </code></pre>
  
  <p>Thank you in advance for all suggestions! </p>