Skip to content

Instantly share code, notes, and snippets.

@rutwick
Created June 12, 2016 12:29
Show Gist options
  • Save rutwick/59cbbb5f1dc4febcebe5f9925660e7b8 to your computer and use it in GitHub Desktop.
Save rutwick/59cbbb5f1dc4febcebe5f9925660e7b8 to your computer and use it in GitHub Desktop.
Read an XML string and write it as a file to current working directory
from flask import Flask
from flask import render_template
from flask import request
from flask import jsonify
from xml.etree import ElementTree as ET
import sys, os
app = Flask(__name__)
@app.route('/')
def hello_world():
xml_string = '<?xml version="1.0" encoding="UTF-8"?><note><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don\'t forget me this weekend!</body></note>'
xml_string_err = '<?xml version="1.0" encoding="UTF-8"?><note><to>Tove</to><from>Jani</Ffrom><heading>Reminder</heading><body>Don\'t forget me this weekend!</body></note>'
try:
x = ET.fromstring(xml_string)
except Exception, Argument:
print 'Houston, problem:', Argument
else:
location = os.getcwd()
filename = 'sample.xml'
with open(filename, 'w') as f:
f.write(xml_string)
return render_template('index.html')
@app.route('/handle', methods=['POST'])
def handle():
user = request.form['key']
return jsonify(got=user)
if __name__ == '__main__':
app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment