0

So I am building a REST API in Python/Flask and I have already handled the part that enables me to read data in JSON format as follows:

data = request.get_json()

I was wondering if there was an xml equivalent to this and if so how can I use it. Please help me.

davidism
  • 121,510
  • 29
  • 395
  • 339
Kelyn Paul
  • 26
  • 1
  • 7

2 Answers2

1

Example:

import xml.etree.ElementTree as ET
tree = ET.parse('items.xml')
root = tree.getroot()

# all items data
print('Expertise Data:')

for elem in root:
   for subelem in elem:
      print(subelem.text)
MrAZ
  • 394
  • 3
  • 13
0

From what you said in the comment, it sounds like you have data in some format, and you want to convert that data into an XML syntax string. Assuming that the data is in a dictionary (based on the code you posted where you get the json), you probably want to use the xml.etree.Elementree package. @MrAZ's answer reads the data, but the documentation also provides a means to write data to a string.

Alternatively, there are third-party packages (not included with Python) which convert json or dict into xml (for example, https://pypi.org/project/dicttoxml/ and https://pypi.org/project/json2xml/ , which can be installed with pip)

mwarrior
  • 499
  • 5
  • 17