Skip to content

Commit d5e535a

Browse files
committed
src - Parse data from ET.Element
- Autodect parser type - Use DOM parser if ET.Element is provided - Update docstring - Refs: #57
1 parent d49ad80 commit d5e535a

File tree

1 file changed

+23
-7
lines changed

1 file changed

+23
-7
lines changed

overpy/__init__.py

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -349,23 +349,39 @@ def from_json(cls, data, api=None):
349349
return result
350350

351351
@classmethod
352-
def from_xml(cls, data, api=None, parser=XML_PARSER_SAX):
352+
def from_xml(cls, data, api=None, parser=None):
353353
"""
354-
Create a new instance and load data from xml object.
354+
Create a new instance and load data from xml data or object.
355+
356+
.. note::
357+
If parser is set to None, the functions tries to find the best parse.
358+
By default the SAX parser is chosen if a string is provided as data.
359+
The parser is set to DOM if an xml.etree.ElementTree.Element is provided as data value.
355360
356361
:param data: Root element
357-
:type data: xml.etree.ElementTree.Element
358-
:param api:
362+
:type data: str | xml.etree.ElementTree.Element
363+
:param api: The instance to query additional information if required.
359364
:type api: Overpass
360-
:param parser: Specify the parser to use(DOM or SAX)
361-
:type parser: Integer
365+
:param parser: Specify the parser to use(DOM or SAX)(Default: None = autodetect, defaults to SAX)
366+
:type parser: Integer | None
362367
:return: New instance of Result object
363368
:rtype: Result
364369
"""
370+
if parser is None:
371+
if isinstance(data, str):
372+
parser = XML_PARSER_SAX
373+
else:
374+
parser = XML_PARSER_DOM
375+
365376
result = cls(api=api)
366377
if parser == XML_PARSER_DOM:
367378
import xml.etree.ElementTree as ET
368-
root = ET.fromstring(data)
379+
if isinstance(data, str):
380+
root = ET.fromstring(data)
381+
elif isinstance(data, ET.Element):
382+
root = data
383+
else:
384+
raise exception.OverPyException("Unable to detect data type.")
369385

370386
for elem_cls in [Node, Way, Relation, Area]:
371387
for child in root:

0 commit comments

Comments
 (0)