使用Django和Python无法显示正确的值

时间:2017-06-16 04:36:39

标签: python xml django

我无法使用Django和Python获取和显示该值。我在下面解释我的代码。

    def viewbook(request):
        doc = minidom.parse("roomlist.xml")
        staffs = doc.getElementsByTagName("location")
        root = []
        for staff in staffs:
            lname=staff.getAttribute("name");
            roomname=staff.getElementsByTagName("roomname")[0]
            seat=staff.getElementsByTagName("noseats")[0]
            project=staff.getElementsByTagName("projectorscreen")[0]
            video=staff.getElementsByTagName("videoconf")[0]
            root.append({'lname':lname,'roomname':roomname,'seat':seat,'project':project,'video':video})
return render(request,'booking/viewbook.html',{'people': root}) 

我在这里从.xml表中获取数据,下面给出了表格。

<?xml version="1.0" ?><roomlist>
  <location name="Bangalore">
    <room id="1uy92j908u092">
      <roomname> Aquarius </roomname>
      <noseats> 10 </noseats>
      <projectorscreen>yes</projectorscreen>
      <videoconf>yes</videoconf>
    </room>
  </location>
<location name="Bhubaneswar"><room id="131198912460"><roomname>cottage</roomname><noseats>5</noseats><projectorscreen>Yes</projectorscreen><videoconf>Yes</videoconf></room></location></roomlist>

这里我试图以表格格式显示值,代码如下所示。

<tr>
            <th>Location Name</th>
            <th>Room Name</th>
            <th>seats</th>
            <th>Projector screen</th>
            <th>Video Conference</th>
        </tr>
    {% for person in people %}
        <tr>
            <td>{{person.lname}}</td>
            <td>{{person.roomname}}</td>
            <td>{{person.seat}}</td>
            <td>{{person.project}}</td>
            <td>{{person.video}}</td>
        </tr>

但在我的情况下,我得到的是以下类型的输出。

Location Name       Room Name    seats   Projector screen    Video Conference
Bangalore   <DOM Element: roomname at 0x7fcc35275440>   <DOM Element: noseats at 0x7fcc35275560>    <DOM Element: projectorscreen at 0x7fcc35275290>    <DOM Element: videoconf at 0x7fcc352757a0>
Bhubaneswar <DOM Element: roomname at 0x7fcc35262cf8>   <DOM Element: noseats at 0x7fcc374fb6c8>    <DOM Element: projectorscreen at 0x7fcc36579128>    <DOM Element: videoconf at 0x7fcc352756c8>

在这里,我需要从XML表中获取所有正确的值。

3 个答案:

答案 0 :(得分:2)

你必须通过xml的firstChild.nodeValuefirstChild.data获取元素值,试试这个:

def viewbook(request):
    doc = minidom.parse("roomlist.xml")
    staffs = doc.getElementsByTagName("location")
    root = []
    for staff in staffs:
        lname=staff.getAttribute("name");
        roomname=staff.getElementsByTagName("roomname")[0].firstChild.nodeValue.strip()
        seat=staff.getElementsByTagName("noseats")[0].firstChild.nodeValue.strip()
        project=staff.getElementsByTagName("projectorscreen")[0].firstChild.nodeValue.strip()
        video=staff.getElementsByTagName("videoconf")[0].firstChild.nodeValue.strip()

答案 1 :(得分:0)

根据文档中的示例,node.data是从仅包含文本的节点获取数据的方式。

def getText(nodelist): 
  rc = [] for node in nodelist: 
  if node.nodeType == node.TEXT_NODE:      
    rc.append(node.data)
  return "".join(rc)

docs

答案 2 :(得分:0)

你可以得到

from xml.dom.minidom import parse
doc = parse("dd.xml")
staffs = doc.getElementsByTagName("location")
root = []
for staff in staffs:
    lname=staff.getAttribute("name");
    roomname=staff.getElementsByTagName("roomname")[0].firstChild.wholeText
    seat=staff.getElementsByTagName("noseats")[0].firstChild.wholeText
    project=staff.getElementsByTagName("projectorscreen")[0].firstChild.wholeText
    video=staff.getElementsByTagName("videoconf")[0].firstChild.wholeText
    root.append({'lname':lname,'roomname':roomname,'seat':seat,'project':project,'video':video})
print root

输出

[{'lname': u'Bangalore', 'roomname': u' Aquarius ', 'video': u'yes', 'project': u'yes', 'seat': u' 10 '}, {'lname': u'Bhubaneswar', 'roomname': u'cottage', 'video': u'Yes', 'project': u'Yes', 'seat': u'5'}]