def _parse_tag(self) -> Tag: """ Parse a tag and its subdocument or value """ tag = Tag() # If tag is a processing tag if self.doc[self.i] == '?': tag.type = Tag.TYPES.PROCESSOR self.i += 1 else: tag.type = Tag.TYPES.NORMAL if not is_name_start(self.doc[self.i]): raise InvalidTagName() tag.name = self._parse_name() tag.attributes = self._parse_attributes() if tag.type == Tag.TYPES.NORMAL: # parse subdocument tag.children, tag.value = self._parse_document(tag.name) return tag
def parse(self, doc: str): """ Parse an XML String """ self.i = 0 self.doc = doc tags, _ = self._parse_document() root = Tag() root.children = tags return root