Python 将xml文件的一部分写入新文件elementree

Python 将xml文件的一部分写入新文件elementree,python,xml,Python,Xml,我正在解析一个xml文件:我希望读入它并将其中一些复制到一个新文件中。我不知道这有多容易?我当前可以使用以下代码复制整个文件: import xml.etree.ElementTree as ET tree = ET.parse('interface_range_test.xml') root = tree.getroot() tree.write('new_file.xml', encoding="utf-8", xml_declaration=True) 有没有办法搜索某些元素并将其写入新

我正在解析一个xml文件:我希望读入它并将其中一些复制到一个新文件中。我不知道这有多容易?我当前可以使用以下代码复制整个文件:

import xml.etree.ElementTree as ET
tree = ET.parse('interface_range_test.xml')
root = tree.getroot()
tree.write('new_file.xml', encoding="utf-8", xml_declaration=True)
有没有办法搜索某些元素并将其写入新文件

最初,我希望创建一个新文件,其中仅包含以下内容作为测试运行:

<COMMAND name="shutdown"
        help="Shutdown the selected interface">
        <CONFIG priority="0x7F01" />
        <ACTION>
        /klas/klish-scripts/interfaces.py conf -i ${iface} --enable 0
        </ACTION>
    </COMMAND>

    <COMMAND name="no shutdown"
        help="Enable the selected interface">
        <CONFIG operation="unset" pattern="shutdown"/>
        <ACTION>
        /klas/klish-scripts/interfaces.py conf -i ${iface} --enable 1
        </ACTION>
    </COMMAND>
这将获得所有相关信息,但也会获得一些额外的动作标记及其文本

{http://clish.sourceforge.net/XMLSchema}ACTION
{'builtin': 'clish_nested_up'}
None
{http://clish.sourceforge.net/XMLSchema}ACTION
{}

/klas/klish-scripts/ifrange.py validate_range --range "${interface_method} ${iface_num} ${range_separator} ${iface_num2}  ${range_separator2} ${interface_method2} ${iface_num3} ${range_separator3} ${iface_num4} ${range_separator4} ${interface_method3} ${iface_num5} ${range_separator5} ${iface_num6} ${range_separator6} ${interface_method4} ${iface_num7} ${range_separator7} ${iface_num8}"

if [[ $? -eq 0 ]]; then
/klas/klish-scripts/ifrange.py run_command --cmdrange "${interface_method} ${iface_num} ${range_separator} ${iface_num2}  ${range_separator2} ${interface_method2} ${iface_num3} ${range_separator3} ${iface_num4} ${range_separator4} ${interface_method3} ${iface_num5} ${range_separator5} ${iface_num6} ${range_separator6} ${interface_method4} ${iface_num7} ${range_separator7} ${iface_num8}" --command "/klas/klish-scripts/interfaces.py conf -i {iface} --enable 0" --klish_config "shutdown" --klish_action "set" --priority "0x7F01"
fi





{http://clish.sourceforge.net/XMLSchema}COMMAND
[('name', 'shutdown'), ('help', 'Shutdown the selected interface')]
{http://clish.sourceforge.net/XMLSchema}CONFIG
[('priority', '0x7F01')]
{http://clish.sourceforge.net/XMLSchema}ACTION
{}

        /klas/klish-scripts/interfaces.py conf -i ${iface} --enable 0

{http://clish.sourceforge.net/XMLSchema}COMMAND
[('name', 'no shutdown'), ('help', 'Enable the selected interface')]
{http://clish.sourceforge.net/XMLSchema}CONFIG
[('pattern', 'shutdown'), ('operation', 'unset')]
{http://clish.sourceforge.net/XMLSchema}ACTION
{}

        /klas/klish-scripts/interfaces.py conf -i ${iface} --enable 1
我可以尝试使用找到的数据创建输出吗?这是一个好方法吗?还是有比搜索每一条数据更好的方法?也许得到了节点,然后是孙子?然而,我似乎无法做到这一点。或者找到shutdown元素并将其全部及其子元素写入文件

我可以这样写节点吗

编辑以显示对文本的更改

for command in commands:
    if 'shutdown' in command.get('name'):
        for i in command:
            i.text = "abc" #modify to taste

        ET.dump(command) # modify to taste
(1) 您无法从ElementTree获得所需的输出,因为您需要的是格式不正确的XML。将
节点包装到根标记中,或者逐个序列化片段并连接

(2) 是的,有一个。在您的情况下,搜索时必须指定名称空间。对根元素下的元素进行简单扫描也就足够了

我的看法:

import xml.etree.ElementTree as ET
tree = ET.parse('interface_range_test.xml')
root = tree.getroot()
# note the explicit namespaces
commands = root.findall('{http://clish.sourceforge.net/XMLSchema}'
                        'VIEW/{http://clish.sourceforge.net/XMLSchema}COMMAND')
for command in commands:
  if 'shutdown' in command.get('name'):
    ET.dump(command) # modify to taste

帮了大忙,谢谢。当你说完全相同的输出是什么意思?您生成的输出看起来很好,而且格式也是我想要的(我认为ns0不会影响文件,为什么它会这样写?),所以基本上我会逐个查找我想要的所有内容并连接到一个文件?因为它说
dump
仅用于调试,所以如何将命令写入文件?它说元素没有写函数。再次感谢。我试图修改文本,但我不知道这是否是一个好方法,编辑到我的文章。现在看来没问题了,我只需要把它放到一个文件中。@Paul:阅读有帮助,因为它们告诉你如何将子树写入字符串。从那一点上说,你是。谢谢,我曾试图将元素转换为elementtree,但出现了错误,我将尝试这种简单的方法。@Paul,很抱歉再次提出这个问题,但你是如何成功地将数据写入新的xml文件的
import xml.etree.ElementTree as ET
tree = ET.parse('interface_range_test.xml')
root = tree.getroot()
# note the explicit namespaces
commands = root.findall('{http://clish.sourceforge.net/XMLSchema}'
                        'VIEW/{http://clish.sourceforge.net/XMLSchema}COMMAND')
for command in commands:
  if 'shutdown' in command.get('name'):
    ET.dump(command) # modify to taste