Skip to content

Commit daa6a72

Browse files
authored
git - Merge pull request #64 from DinoTools/from_xml_element
From xml element Fixes #57
2 parents d49ad80 + 5acb43f commit daa6a72

File tree

2 files changed

+45
-7
lines changed

2 files changed

+45
-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:

tests/test_xml.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,28 @@ def test_way_missing_data(self):
171171
overpy.Way.from_xml(node)
172172

173173

174+
class TestParser(BaseTestNodes):
175+
def test_exception(self):
176+
with pytest.raises(overpy.exception.OverPyException):
177+
overpy.Result.from_xml(123)
178+
179+
def test_xml_element(self):
180+
import xml.etree.ElementTree as ET
181+
data = read_file("xml/node-01.xml")
182+
root = ET.fromstring(data)
183+
result = overpy.Result.from_xml(root)
184+
185+
assert isinstance(result, overpy.Result)
186+
self._test_node01(result)
187+
188+
def test_xml_autodetect_parser(self):
189+
data = read_file("xml/node-01.xml")
190+
result = overpy.Result.from_xml(data)
191+
192+
assert isinstance(result, overpy.Result)
193+
self._test_node01(result)
194+
195+
174196
class TestRemark(object):
175197
def test_remark_runtime_error(self):
176198
api = overpy.Overpass()

0 commit comments

Comments
 (0)