如何使用python将XML文件中的节点拆分为两个节点

时间:2017-05-09 19:48:12

标签: python xml elementtree

假设我有一个这样的XML文件:

"react": "16.0.0-alpha.6",
"react-native": "https://github.com/expo/react-native/archive/sdk-16.0.0.tar.gz",

我想拆分" group"的节点"新加坡"之后的两个,如:

<?xml version="1.0"?>
<data>
    <group>
        <country name="Liechtenstein">
            <rank>1</rank>
            <year>2008</year>
        </country>
        <country name="Singapore">
            <rank>4</rank>
            <year>2011</year>
        </country>
        <country name="Panama">
            <rank>68</rank>
            <year>2011</year>
        </country>
    </group>
</data>

是否可以在python中使用ElementTree来做到这一点?

非常感谢。

2 个答案:

答案 0 :(得分:1)

是的,你可以,但使用lxml库更容易:

from lxml import etree

content = u"""\
<data>
    <group>
        <country name="Liechtenstein">
            <rank>1</rank>
            <year>2008</year>
        </country>
        <country name="Singapore">
            <rank>4</rank>
            <year>2011</year>
        </country>
        <country name="Panama">
            <rank>68</rank>
            <year>2011</year>
        </country>
    </group>
</data>"""

root = etree.XML(content)

# insert a new "<group>" element at the end of "<data>" children:
new_group = etree.SubElement(root, "group")

# find "Panama"
panama = root.xpath('//country[@name="Panama"]')[0]

# move "Panama" into the new group
new_group.append(panama)

你会得到:

<data>
    <group>
        <country name="Liechtenstein">
            <rank>1</rank>
            <year>2008</year>
        </country>
        <country name="Singapore">
            <rank>4</rank>
            <year>2011</year>
        </country>
        </group>
<group><country name="Panama">
            <rank>68</rank>
            <year>2011</year>
        </country>
    </group></data>

修改

查找“新加坡”之后的所有国家

other_nodes = root.xpath('//country[count(preceding-sibling::country[@name = "Singapore"]) != 0]')

将找到的节点移动到新组

new_group.extend(other_nodes)

答案 1 :(得分:0)

您可以尝试遍历每个节点,然后将名称为Panama的节点存储到temp var并创建新的Element并添加到根树。

import xml.etree.ElementTree as ET

items = """
<data>
    <group>
        <country name="Liechtenstein">
            <rank>1</rank>
            <year>2008</year>
        </country>
        <country name="Singapore">
            <rank>4</rank>
            <year>2011</year>
        </country>
        <country name="Panama">
            <rank>68</rank>
            <year>2011</year>
        </country>
    </group>
</data>
"""

root = ET.fromstring(items)
node_panama = None
for group in root:
    for country in group.findall('country'):
        name = country.attrib['name']
        if name == 'Panama':
            node_panama = country
            group.remove(country)

new_group = ET.Element('group')
new_group.append(node_panama)

root.append(new_group)
ET.dump(root)

将输出

<data>
    <group>
        <country name="Liechtenstein">
            <rank>1</rank>
            <year>2008</year>
        </country>
        <country name="Singapore">
            <rank>4</rank>
            <year>2011</year>
        </country>
    </group>
    <group>
        <country name="Panama">
            <rank>68</rank>
            <year>2011</year>
        </country>
    </group>
</data>