【问题标题】:Cannot display the proper value using Django and Python无法使用 Django 和 Python 显示正确的值
【发布时间】:2017-06-16 04:36:39
【问题描述】:

我无法使用 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 表单中获取所有正确的值。

【问题讨论】:

  • 你的退货单在哪里?
  • 查看 minidom 的 API 文档,了解如何提取 'name' 等属性值和节点中的值。
  • 可以修改吗?
  • @Arun : 请查看更新后的帖子。

标签: python xml django


【解决方案1】:

你必须通过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()

【讨论】:

  • @satya 酷,很高兴它有效,获得价值后更好strip()
【解决方案2】:

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

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

docs

【讨论】:

    【解决方案3】:

    你可以得到

    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'}]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-11
      • 1970-01-01
      • 1970-01-01
      • 2018-02-16
      • 2014-08-24
      • 2015-05-09
      相关资源
      最近更新 更多