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 .
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
< th>Mission ID
{% for elem in elem_list %}
< br />
{% 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">
Name | Parent Task ID | |
---|---|---|
{{ elem.name }} | {{ elem.mission_id }} | {{ elem.parent_task_id }}< /td> |
{% for elem in elem_list %}
{% 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
Name | Mission ID | Parent Task ID< /th> |
---|---|---|
{{ elem.name }} | {{ elem.mission_id }} | {{ elem.parent_task_id }} |