如何从 Django/Python 中的 XML 标签之间的 XML 文档中提取数据?

发布于 2024-12-05 00:45:28 字数 2077 浏览 0 评论 0 原文

我有一个外部 xml 文件,正在我的 views.py 文件中加载

def test(request):

    url = urllib2.urlopen("http://someurl.com?xml")
    dom = minidom.parse(url)

    groups = dom.getElementsByTagName("group")

    deal_holder = []

    #    Iterate over each DOM group element:
    for group in groups:
        # Iterate over each child node
        for groupChild in group.childNodes:
            deal_holder.append(groupChild)

    return render_to_response('folder/test.html', {'deal_holder':deal_holder})

这是加载的 XML 文件的样子:

<page>
    <site>
        <siteid>25550</siteid>
        <sitename>
            <![CDATA[ Some Text Here ]]>
        </sitename>
        <sitelink>
            http://somelinkehere.com
        </sitelink>
        <timezone>
            <![CDATA[ Pacific Time ]]>
        </timezone>
    </site>
    <groups>
        <enablefeaturedgroup>OFF</enablefeaturedgroup>
        <group>
            <groupid>467246</groupid>
            <groupname>
                <![CDATA[ Today's Deal ]]>
            </groupname>
            <groupdescription>
                <![CDATA[ ]]>
            </groupdescription>
            </group>
            <group>
            <groupid>467247</groupid>
            <groupname>
                <![CDATA[ Past Deals ]]>
            </groupname>
            <groupdescription>
                <![CDATA[ ]]>
            </groupdescription>
        </group>
    </groups>
</page>

问题是我见过的所有示例都使用类似于我的内容正在使用,但它们通常具有如下所示的 XML 标记: 并能够从 day="Wed"date="14 Sep 2011" 等内容中检索信息, low="56" 等...但我想要检索的信息实际上是在标签之间,例如 25550

任何建议或信息将不胜感激。

I have an external xml file that I am loading in my views.py file

def test(request):

    url = urllib2.urlopen("http://someurl.com?xml")
    dom = minidom.parse(url)

    groups = dom.getElementsByTagName("group")

    deal_holder = []

    #    Iterate over each DOM group element:
    for group in groups:
        # Iterate over each child node
        for groupChild in group.childNodes:
            deal_holder.append(groupChild)

    return render_to_response('folder/test.html', {'deal_holder':deal_holder})

This is what the loaded XML file looks like:

<page>
    <site>
        <siteid>25550</siteid>
        <sitename>
            <![CDATA[ Some Text Here ]]>
        </sitename>
        <sitelink>
            http://somelinkehere.com
        </sitelink>
        <timezone>
            <![CDATA[ Pacific Time ]]>
        </timezone>
    </site>
    <groups>
        <enablefeaturedgroup>OFF</enablefeaturedgroup>
        <group>
            <groupid>467246</groupid>
            <groupname>
                <![CDATA[ Today's Deal ]]>
            </groupname>
            <groupdescription>
                <![CDATA[ ]]>
            </groupdescription>
            </group>
            <group>
            <groupid>467247</groupid>
            <groupname>
                <![CDATA[ Past Deals ]]>
            </groupname>
            <groupdescription>
                <![CDATA[ ]]>
            </groupdescription>
        </group>
    </groups>
</page>

The problem is that all of the examples I've seen use something like what I'm using except that they usually have XML tags that look like this: <weather:forecast day="Wed" date="14 Sep 2011" low="56" high="72" text="AM Clouds/PM Sun" code="30"/> and are able to retrieve the information from stuff like the day="Wed", date="14 Sep 2011", low="56" etc... but the info I want to retrieve is actually between the tags such as <siteid>25550</siteid>

Any advice or info would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ゞ记忆︶ㄣ 2024-12-12 00:45:28

使用 minidom 与 javascript 非常相似。

from xml.dom import minidom
from StringIO import StringIO
a = """<page>
    <site>
        <siteid>25550</siteid>
        <sitename>
            <![CDATA[ Some Text Here ]]>
        </sitename>
        <sitelink>
            http://somelinkehere.com
        </sitelink>
        <timezone>
            <![CDATA[ Pacific Time ]]>
        </timezone>
    </site>
    <groups>
        <enablefeaturedgroup>OFF</enablefeaturedgroup>
        <group>
            <groupid>467246</groupid>
            <groupname>
                <![CDATA[ Today's Deal ]]>
            </groupname>
            <groupdescription>
                <![CDATA[ ]]>
            </groupdescription>
            </group>
            <group>
            <groupid>467247</groupid>
            <groupname>
                <![CDATA[ Past Deals ]]>
            </groupname>
            <groupdescription>
                <![CDATA[ ]]>
            </groupdescription>
        </group>
    </groups>
</page>
"""
tree = minidom.parse(StringIO(a))
groups = tree.getElementsByTagName("group")

如果您使用 urllib,则不需要使用 StringIO,因为 minidomparse 方法需要一个类似文件的对象 (urllib .urlopen 返回的就是这个)。

我建议不要将此列表传递给 django 模板系统。你应该进一步解析它。

#    Iterate over each DOM group element:
group_dictionaries = []
for group in groups:
    group_dict = {}
    # Iterate over each child node
    # instead of for loop maybe print groupChildNodes[0] for groupid
    # print groupChildNodes[1] for groupname
    for groupChild in group.ChildNodes:
        # do something with each node
        group_dict[groupChild.tagName] = groupChild.data
    group_dictionaries.append(group_dict)

  Now in the template:
  {% for group in group_dictionaries %}
      {{ group.groupid }}
      {{ group.groupname }}
      etc.
  {% endfor %}

您可以将它们的值保存在字典列表中。

Using minidom is quite similar to javascript.

from xml.dom import minidom
from StringIO import StringIO
a = """<page>
    <site>
        <siteid>25550</siteid>
        <sitename>
            <![CDATA[ Some Text Here ]]>
        </sitename>
        <sitelink>
            http://somelinkehere.com
        </sitelink>
        <timezone>
            <![CDATA[ Pacific Time ]]>
        </timezone>
    </site>
    <groups>
        <enablefeaturedgroup>OFF</enablefeaturedgroup>
        <group>
            <groupid>467246</groupid>
            <groupname>
                <![CDATA[ Today's Deal ]]>
            </groupname>
            <groupdescription>
                <![CDATA[ ]]>
            </groupdescription>
            </group>
            <group>
            <groupid>467247</groupid>
            <groupname>
                <![CDATA[ Past Deals ]]>
            </groupname>
            <groupdescription>
                <![CDATA[ ]]>
            </groupdescription>
        </group>
    </groups>
</page>
"""
tree = minidom.parse(StringIO(a))
groups = tree.getElementsByTagName("group")

Using StringIO is not required if you are using urllib, because the minidom's parse method expects a file-like object (urllib.urlopen returns just that).

I'd advise against passing this list to the django templating system. You should parse it further.

#    Iterate over each DOM group element:
group_dictionaries = []
for group in groups:
    group_dict = {}
    # Iterate over each child node
    # instead of for loop maybe print groupChildNodes[0] for groupid
    # print groupChildNodes[1] for groupname
    for groupChild in group.ChildNodes:
        # do something with each node
        group_dict[groupChild.tagName] = groupChild.data
    group_dictionaries.append(group_dict)

  Now in the template:
  {% for group in group_dictionaries %}
      {{ group.groupid }}
      {{ group.groupname }}
      etc.
  {% endfor %}

You could save them values in a list of dictionaries.

半葬歌 2024-12-12 00:45:28

使用 lxml 你可以这样做:

import lxml.etree

tree = lxml.etree.parse("http://someurl.com")
sites = tree.xpath("//site")

for site in sites:
    siteid = site.find("siteid").text
    print siteid

With lxml you could do something like this:

import lxml.etree

tree = lxml.etree.parse("http://someurl.com")
sites = tree.xpath("//site")

for site in sites:
    siteid = site.find("siteid").text
    print siteid
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文