How to search for a child key in an xml and pass it to another parent with Python/bs4?
<p>Here is a sample of the xml I am working with:</p>

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

<p>I am trying to look through each key under "bare" and if the first word matches an app name, move the key/value under the app key (as child).  So for example, plus.root would be removed from the bare section and added under the "app name=plus section".  If the app name does not exist, the key should be left alone under the bare section.</p>

<p>Currently my code looks like this, but I'm having trouble figuring out properly do this.</p>

<pre><code>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())
</code></pre>

<p>Is there a better way to do this? </p>