Python在所需标签下以XML编写同级 [英] Python write siblings in XML below desired tag

查看:73
本文介绍了Python在所需标签下以XML编写同级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在< VIDPOM> 10</VIDPOM> 标签

我的XML如下:

<ZAP>
    <N_ZAP>999</N_ZAP>
    <SLUCH>
      <IDCASE>100100100</IDCASE>
      <USL_OK>3</USL_OK>
      <VIDPOM>10</VIDPOM>
      <IDSP>99</IDSP>
      <USL>
        <IDSERV>123456789</IDSERV>
        <DATE_IN>2020-12-01</DATE_IN>
      </USL>
    </SLUCH>
</ZAP>

但是我想要这样:

<ZAP>
    <N_ZAP>999</N_ZAP>
    <SLUCH>
      <IDCASE>100100100</IDCASE>
      <USL_OK>3</USL_OK>
      <VIDPOM>10</VIDPOM>
      <MY_CUSTOM_TAG>TEXT IS HERE</MY_CUSTOM_TAG>
      <IDSP>99</IDSP>
      <USL>
        <IDSERV>123456789</IDSERV>
        <DATE_IN>2020-12-01</DATE_IN>
      </USL>
    </SLUCH>
</ZAP>

我的代码是:

import xml.etree.ElementTree as ET


tree = ET.parse('file.xml')
root = tree.getroot()

for SLUCH in root.iter('SLUCH'):
    IDCASE = SLUCH.find('IDCASE')
    VIDPOM = SLUCH.find('VIDPOM')
    print(IDCASE.text)
    print(VIDPOM.text)
    print()

    if IDCASE.text == "100100100":
        print('HERE')

        new_tag = ET.SubElement(VIDPOM, 'MY_CUSTOM_TAG')
        new_tag.text = 'TEXT IS HERE'
        
        tree.write('file_new.xml')

输出为:

<ZAP>
    <N_ZAP>999</N_ZAP>
    <SLUCH>
      <IDCASE>100100100</IDCASE>
      <USL_OK>3</USL_OK>
      <VIDPOM>10
        <MY_CUSTOM_TAG>TEXT IS HERE</MY_CUSTOM_TAG>
      </VIDPOM>
      <IDSP>99</IDSP>
      <USL>
        <IDSERV>123456789</IDSERV>
        <DATE_IN>2020-12-01</DATE_IN>
      </USL>
    </SLUCH>
</ZAP>

提前谢谢!

推荐答案

您可以在 SLUCH

index = list(SLUCH).index(VIDPOM)  # deprecated: SLUCH.getchildren().index(VIDPOM)

,然后您可以在 VIDPOM

SLUCH.insert(index+1, new_tag)


要格式化诸如 VIDPOM 之类的新元素(相同的缩进),您可以复制 tail


To format new element like VIDPOM (the same indentations) you can copy tail

new_tag.tail = VIDPOM.tail


最少的工作代码-直接在代码中包含数据.


Minimal working code - with data directly in code.

text ='''
<ZAP>
    <N_ZAP>999</N_ZAP>
    <SLUCH>
      <IDCASE>100100100</IDCASE>
      <USL_OK>3</USL_OK>
      <VIDPOM>10</VIDPOM>
      <IDSP>99</IDSP>
      <USL>
        <IDSERV>123456789</IDSERV>
        <DATE_IN>2020-12-01</DATE_IN>
      </USL>
    </SLUCH>
</ZAP>
'''

import xml.etree.ElementTree as ET

#tree = ET.parse('file.xml')
#root = tree.getroot()
root = ET.fromstring(text)

for SLUCH in root.iter('SLUCH'):
    VIDPOM = SLUCH.find('VIDPOM')

    new_tag = ET.Element('MY_CUSTOM_TAG')
    new_tag.text = 'TEXT IS HERE'
    new_tag.tail = VIDPOM.tail  # copy text after `tag`

    index = list(SLUCH).index(VIDPOM)
    #index = SLUCH.getchildren().index(VIDPOM)  # deprecated
    SLUCH.insert(index+1, new_tag)

print(ET.tostring(root).decode())

结果:

<ZAP>
    <N_ZAP>999</N_ZAP>
    <SLUCH>
      <IDCASE>100100100</IDCASE>
      <USL_OK>3</USL_OK>
      <VIDPOM>10</VIDPOM>
      <MY_CUSTOM_TAG>TEXT IS HERE</MY_CUSTOM_TAG>
      <IDSP>99</IDSP>
      <USL>
        <IDSERV>123456789</IDSERV>
        <DATE_IN>2020-12-01</DATE_IN>
      </USL>
    </SLUCH>
</ZAP>

这篇关于Python在所需标签下以XML编写同级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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