ElementTree-将子元素追加到元素中的问题 [英] ElementTree - Issue in appending the subelement to an element

查看:276
本文介绍了ElementTree-将子元素追加到元素中的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个元素的子元素,该元素位于此处元素国家/地区新加坡旁边。

I want to create subelement to an element that comes next to the element country singapore here.

假设我的test.xml文件如下所示

Suppose my test.xml file looks like this

<?xml version="1.0" encoding="UTF-8"?>

<data>
    <country name="Malaysia" tst="bh">
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Singapore" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <district>
        <A name="test">
        </A>
    </district>
    <country name="Singapore" tst="ab">
        <rank updated="yes">5</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
   <district>
        <B name="test">
        </B>
    </district>
</data>

在上面的示例中,我想创建元素element的子元素,但是上面的元素应该是国家/地区新加坡。
应该是

In the above example,I want to create subelement to element district but the element present above should be country "singapore". It should be

<district>
   <t1 name="t1>
   </t1>
     <B name="test">
    </B>
</district>

import xml.etree.ElementTree as et


tree = et.parse("test.xml")
root = tree.getroot()

country = root.find(".//country[@name='Singapore']")

et.subelement(country,"add new subelement")

我可以在国家/地区元素中添加subelement。但是我不能带区

I am able to add subelement to country element. But i couldn't take the district element below the country "singapore".

有人可以在这里帮助我吗?

Can anyone please help me here??

推荐答案

这是使用ElementTree完成的方法。

Here is how it can be done with ElementTree.

import xml.etree.ElementTree as ET

root = ET.parse("country.xml").getroot()

# A list of all children of the root element (in document order)
children = list(root)

# Find the Singapore 'country' element
sing = root.find(".//country/[@name='Singapore']")

# Get the index of the 'country' element 
ix = children.index(sing)

# Find the wanted 'district' sibling element (position ix+1 in the list)
district = children[ix+1]

# Create a new 't1' element and add to 'district'
t1 = ET.Element("t1", name="t1")
district.insert(0, t1)

print(ET.tostring(root).decode("UTF-8"))

输出:

<data>
    <country name="Malaysia" tst="bh">
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor direction="E" name="Singapore" />
        <neighbor direction="W" name="Switzerland" />
    </country>
    <district>
        <A name="test">
        </A>
    </district>
    <country name="Singapore" tst="ab">
        <rank updated="yes">5</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor direction="N" name="Malaysia" />
    </country>
   <district>
        <t1 name="t1" /><B name="test">        <!-- New element added here --> 
        </B>
    </district>
</data>

这篇关于ElementTree-将子元素追加到元素中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆