code: def build_tree(self):
          if self.built:
              return
          self.doc_root = self.root.element()
          for key in self.sorted_fields():
              if key not in self._fields:
                  continue
              field = self._fields[key]
              if field != self.root:
                  if isinstance(field, XmlModel):
                      field.build_tree()
                      if (self.drop_empty and field.drop_empty
                          and len(field.doc_root) == 0):
                          continue
                      self.doc_root.append(field.doc_root)
                  elif isinstance(field, list):
                      for item in field:
                          if isinstance(item, XmlField):
                              ele = item.element()
                              if self.drop_empty and len(ele) == 0:
                                  continue
                              self.doc_root.append(ele)
                          elif isinstance(item, XmlModel):
                              item.build_tree()
                              if self.drop_empty and len(item.doc_root) == 0:
                                  continue
                              self.doc_root.append(item.doc_root)
                          elif isinstance(item, (six.text_type, six.string_types)):
                              ele = etree.fromstring(clean_xml(item))
                              self.doc_root.append(ele)
                          item = None
                  elif (field.parent or self.root.name) == self.root.name:
                      ele = field.element()
                      if self.drop_empty and len(ele) == 0 and not ele.text:
                          continue
                      ele = field.element(parent=self.doc_root)
                  else:
                      nodes = [n for n in self.doc_root.iterdescendants(
                                              tag=field.parent)]
                      if nodes:
                          ele = field.element()
                          if (self.drop_empty and len(ele) == 0 and not ele.text):
                              continue
                          ele = field.element(parent=nodes[0])
          self.built = True