0

我以 xml 格式返回了以下类型的数据(返回了许多房间;这是我返回的数据的一个示例):

<?xml version="1.0" encoding="UTF-8"?>
<rooms>
    <total-results>1</total-results>
    <items-per-page>1</items-per-page>
    <start-index>0</start-index>
    <room>
        <id>xxxxxxxx</id>
        <etag>5</etag>
        <link rel="http://schemas.com.mysite.building" title="building" href="https://mysite.me.myschool.edu:8443/ess/scheduleapi/v1/buildings/yyyyyyyyy"/>
        <name>1.306</name>
        <status>active</status>
        <link rel="self" title="self" href="https://mysite.me.myschool.edu:8443/ess/scheduleapi/v1/rooms/aaaaaaaaa">
    </room>
</rooms>

如果 nodeType == node.TEXT_NODE,我似乎可以访问数据(所以我可以看到我有 1.306 房间)。此外,我似乎能够访问 nodeName链接,但我真的需要知道那个房间是否在我可接受的建筑物之一中,所以我需要能够到达该行的其余部分以查看 yyyyyyyyy。有人可以建议吗?

好的,@vezult,这是我最终使用 ElementTree 提出的(工作代码!),正如你所建议的。这可能不是最pythonic(或ElementTree-ic?)的方式,但它似乎有效。我很高兴现在可以访问我的 xml 中的每一个片段的 .tag、.attrib 和 .text。我欢迎任何关于如何使它变得更好的建议。

# We start out knowing our room name and our building id.  However, the same room can exist in many buildings.
# Examine the rooms we've received and get the id of the one with our name that is also in our building.

# Query the API for a list of rooms, getting u back.

request = build_request(resourceUrl)
u = urllib2.urlopen(request.to_url())
mydata = u.read()

root = ElementTree.fromstring(mydata)
print 'tree root', root.tag, root.attrib, root.text
for child in root:
    if child.tag == 'room':   
        for child2 in child:
            # the id tag comes before the name tag, so hold on to it
            if child2.tag == "id":
                hold_id = child2.text
            # the building link comes before the room name, so hold on to it
            if child2.tag == 'link':                            # if this is a link
                if "building" in child2.attrib['href']:         # and it's a building link
                    hold_link_data = child2.attrib['href']
            if child2.tag == 'name':
                if (out_bldg in hold_link_data and  # the building link we're looking at has our building in it  
                    (in_rm == child2.text)):        # and this room name is our room name
                    out_rm = hold_id
                    break  # get out of for-loop
4

1 回答 1

3

您没有提供您正在使用的库的指示,所以我假设您使用的是标准 pythonElementTree模块。在这种情况下,请执行以下操作:

from xml.etree import ElementTree

tree = ElementTree.fromstring("""<?xml version="1.0" encoding="UTF-8"?>
<rooms>
    <total-results>1</total-results>
    <items-per-page>1</items-per-page>
    <start-index>0</start-index>
    <room>
        <id>xxxxxxxx</id>
        <etag>5</etag>
        <link rel="http://schemas.com.mysite.building" title="building" href="https://mysite.me.myschool.edu:8443/ess/scheduleapi/v1/buildings/yyyyyyyyy" />
        <name>1.306</name>
        <status>active</status>
        <link rel="self" title="self" href="https://mysite.me.myschool.edu:8443/ess/scheduleapi/v1/rooms/aaaaaaaaa" />
    </room>
</rooms>
""")

# Select the first link element in the example XML
for node in tree.findall('./room/link[@title="building"]'):
    # the 'attrib' attribute is a dictionary containing the node attributes
    print node.attrib['href']
于 2012-10-04T19:52:48.830 回答