I am new to Python. I want to display this XML string as a list in a template.
xmlData = """
" ""
I just want to display it as a list.
I import cElementTree
from xml.etree import cElementTree as ET
I also did:
xmlList = ET.fromstring(xmlData)
But I don’t know how to display it. I want to display it like this Stuff.
print xmlList.name
print xmlList.mission_id
print xmlList.parent_task_id
Please help me know the correct syntax .
You can use this code example:
p>
from xml.etree import cElementTree as ET
xml = ET.fromstring(xmlData)
for child in xml.iter('field'):
print child.tag, child.attrib, child.text
Iterate over all XML elements named field and print their tags, attributes and text values to the console.
View< code>xml.etree documentation for more samples.
Django view
In order to present the parsed XML data as a view in a Django application, you need view and template.
Assuming that a Django application named app is installed in your project.
application/views.py
from xml.etr ee import cElementTree as ET
from django.http import HttpResponse
from django.shortcuts import render
from django.template import Context, loader
< br />def xml_view(request):
xmlData = """
< br />
"""
xml = ET.fromstring(xmlData)
fields = []
for obj in xml.iter("object"):
fields.append({'name' : obj.find("field[@nam e='name']").text,
'mission_id': obj.find("field[@name='mission_id']").text,
'parent_task_id': obj.find( "field[@name='parent_task_id']").text,
})
t = loader.get_template('your_app/xml_view.html')
c = Context ({'elem_list': fields})
return HttpResponse(t.render(c))
application/template/application/xml_view.html
Name |
< th>Mission ID
Parent Task ID |
---|
{% for elem in elem_list %}
< br /> {{ elem.name }} |
{{ elem.mission_id }} |
{{ elem.parent_task_id }}< /td> |
{% endfor %}
I am new to Python. I want to display this XML string as a list in the template.
xmlData = """
< django-objects version="1.0">
I just want to display it as a list.
I import cElementTree
from xml.etree import cElementTree as ET
I also do Out:
xmlList = ET.fromstring(xmlData)
But I don’t know how to show it. I want to show something like this.
print xmlList.name
print xmlList.mission_id
print xmlList.parent_task_id
Please help me know the correct syntax.
< /p>
You can use this code example:
from xml.etree import cElementTree as ET
xml = ET.fromstring(xmlData)
for child in xml.iter('field'):
print child.tag, child.attrib, child.text
Iterate all the XML elements named field and print their tags, attributes and text values to the console.
Check out the xml.etree
documentation for more samples.
Django view
In order to present the parsed XML data as a view in a Django application, you need view and template.
Assuming that your project has the name app’s Django application.
application/views.py
from xml.etree import cElementTree as ET
from django.http import HttpResponse
from django.shortcuts import render
from django.template import Context, loader
def xml_view(request):
xmlData = """
foo
1< br /> 20
bar
2
10
"""
xml = ET.fromstring(xmlData)
fields = []
for obj in xml.iter("object"):
fields.append({'name': obj.find("field[@name='name ']").text,
'mission_id': obj.find("field[@name='mission_id']").text,
'parent_task_id': obj.find("field[@ name='parent_task_id']").text,
})
t = loader.get_template('your_app/xml_view.html')
c = Context({'elem_list': fields})
return HttpResponse(t.render (c))
application/template/application/xml_view.html
Name |
Mission ID |
Parent Task ID< /th> |
---|
{% for elem in elem_list %}
{{ elem.name }} |
{{ elem.mission_id }} |
{{ elem.parent_task_id }} |
{% endfor% }
WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 6055 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC