将xml字符串转换为Python列表

前端之家收集整理的这篇文章主要介绍了将xml字符串转换为Python列表前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我是 Python的新手.我想将此XML字符串显示为模板中的列表.

xmlData = """<?xml version="1.0" encoding="utf-8"?> 
<django-objects version="1.0">
<object model="task.task" pk="4">
<field name="name" type="CharField">New Task</field>
<field name="mission_id" type="IntegerField">2</field>
<field name="parent_task_id" type="IntegerField">0</field>
</object>
<object model="task.task" pk="5">
<field name="name" type="CharField">New Task</field>
<field name="mission_id" type="IntegerField">2</field>
<field name="parent_task_id" type="IntegerField">0</field>
</object>
<object model="task.task" pk="6">
<field name="name" type="CharField">New ask</field>
<field name="mission_id" type="IntegerField">2</field>
<field name="parent_task_id" type="IntegerField">0</field>
</object>
<object model="task.task" pk="7">
<field name="name" type="CharField">New Task</field>
<field name="mission_id" type="IntegerField">2</field>
<field name="parent_task_id" type="IntegerField">0</field>
</object></django-objects> """

我只想将其显示为列表.
我导入cElementTree

from xml.etree import cElementTree as ET

我也做了:

xmlList = ET.fromstring(xmlData)

但我不知道如何展示它.我想展示这样的东西.

print xmlList.name
print xmlList.mission_id
print xmlList.parent_task_id

请帮我知道正确的语法.

解决方法

您可以使用此代码示例:

from xml.etree import cElementTree as ET
xml = ET.fromstring(xmlData)

for child in xml.iter('field'):
    print child.tag,child.attrib,child.text

迭代所有名为field的XML元素,并将其标记,属性和文本值打印到控制台.

查看xml.etree documentation了解更多样品.

Django视图

为了将解析的XML数据呈现为Django应用程序中的视图,您需要@L_404_2@和template.

假设您的项目中安装了名为app的Django应用程序.

应用程序/ 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 = """<?xml version="1.0" encoding="utf-8"?> 
    <django-objects version="1.0">
    <object model="task.task" pk="4">
    <field name="name" type="CharField">foo</field>
    <field name="mission_id" type="IntegerField">1</field>
    <field name="parent_task_id" type="IntegerField">20</field>
    </object>
    <object model="task.task" pk="7">
    <field name="name" type="CharField">bar</field>
    <field name="mission_id" type="IntegerField">2</field>
    <field name="parent_task_id" type="IntegerField">10</field>
    </object></django-objects>"""

    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))

应用程序/模板/应用/ xml_view.html

<html lang="en">
  <body>
    <table>
      <tr>
        <th>Name</th>
        <th>Mission ID</th>
        <th>Parent Task ID</th>
      </tr>
      {% for elem in elem_list %}
      <tr>
        <td>{{ elem.name }}</td>
        <td>{{ elem.mission_id }}</td>
        <td>{{ elem.parent_task_id }}</td>
      </tr>
      {% endfor %}
  </body>
</html>

猜你在找的XML相关文章