如何在xml中搜索子键并使用Python / bs4将其传递给另一个父键?

时间:2016-03-08 16:39:53

标签: python xml xml-parsing beautifulsoup

以下是我正在使用的xml示例:

<bare>
    <key name="plus.root" value="/apps/mobile/plus"/>
    <key name="local.root" value="/apps/net/plus"/>
    <key name="slack.messaging.root" value="/apps/root/docs"/>
</bare>>
<app name="social">
    <key name="social.password" value="secret">
    <key name="user" value = "secret">
</app>
<app name="plus">
    <key name="user" value = "secret">
</app>

我正试图通过&#34;裸露&#34;如果第一个单词与应用程序名称匹配,请移动app键下的键/值(作为子项)。因此,例如,plus.root将从裸部分中删除并添加到&#34; app name = plus部分&#34;。如果应用程序名称不存在,则应将该密钥留在裸部分下。

目前我的代码看起来像这样,但是我无法正确搞清楚这一点。

from bs4 import BeautifulSoup, Tag

soup = BeautifulSoup(data, "xml")

apps = soup.find("app")
bare = soup.root.bare

# loop over all the "key's under "bare"
for key in bare.find_all("key"):
    app_name = key["name"].split(".")[0]

    # find apps that match name of the bare key
    app = apps.find("app", {"name": app_name})

    #if we find any, ???append the key to the app???? then remove the key from the bare section
    if app:
        key = key.extract()
        app.append(key)
        # remove "bare"
        bare.extract()

print(soup.prettify())

有更好的方法吗?

1 个答案:

答案 0 :(得分:0)

这是一个让您入门的工作示例。在这里,我们正在移动&#34;裸露的#34;在一个单独的应用程序&#34;标签,按应用名称分组:

from bs4 import BeautifulSoup, Tag

data = """
<root>
    <bare>
        <key name="plus.root" value="/apps/mobile/plus"/>
        <key name="local.root" value="/apps/net/plus"/>
        <key name="slack.messaging.root" value="/apps/root/docs"/>
    </bare>
    <apps/>
</root>
"""
soup = BeautifulSoup(data, "xml")

apps = soup.find("apps")
bare = soup.root.bare
# loop over all the "key"s under "bare"
for key in bare.find_all("key"):
    app_name = key["name"].split(".")[0]

    # find app and create if not exists
    app = apps.find("app", {"name": app_name})
    if not app:
        app = soup.new_tag("app")
        app.attrs["name"] = app_name
        apps.append(app)

    # remove the key from "bare" and append to a specific app
    key = key.extract()
    app.append(key)

# remove "bare"
bare.extract()

print(soup.prettify())

结果如下:

<?xml version="1.0" encoding="utf-8"?>
<root>
 <apps>
  <app name="plus">
   <key name="plus.root" value="/apps/mobile/plus"/>
  </app>
  <app name="local">
   <key name="local.root" value="/apps/net/plus"/>
  </app>
  <app name="slack">
   <key name="slack.messaging.root" value="/apps/root/docs"/>
  </app>
 </apps>
</root>