Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def parse_xml(file_path):
builder = _TreeBuilder()
parser = DefusedXMLParser(target=builder, forbid_dtd=False, forbid_entities=True, forbid_external=True)
builder.parser = weakref.proxy(parser)
tree = ElementTree.parse(file_path, parser=parser)
# ElementTree does not implement parent attributes: add parent property to every element
parent_map = {child: parent
for parent in tree.getiterator()
for child in parent}
for p in tree.getiterator():
p.parent = parent_map.get(p)
return tree
def start(self, tag, attrs):
elem = super(_TreeBuilder, self).start(tag, attrs)
elem.line = self.parser.parser.CurrentLineNumber
elem.column = self.parser.parser.CurrentLineNumber
return elem