久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

python xml.etree.ElementTree 附加到子元素

python xml.etree.ElementTree append to subelement(python xml.etree.ElementTree 附加到子元素)
本文介紹了python xml.etree.ElementTree 附加到子元素的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我正在嘗試使用 xml.etree.ElementTree 來(lái)解析一個(gè) xml 文件,找到一個(gè)特定的標(biāo)簽,將一個(gè)子附加到該標(biāo)簽,將另一個(gè)子附加到新創(chuàng)建的標(biāo)簽,并將文本添加到后一個(gè)子.

I am trying to use xml.etree.ElementTree to parse a xml file, find a specific tag, append a child to that tag, append another child to the newly created tag and add text to the latter child.

我的 XML:

<root>
<a>
    <b>
      <c>text1</c>
    </b>
    <b>
      <c>text2</c>
   </b>
</a>
</root>    

所需的 XML:

<root>
<a>
    <b>
      <c>text1</c>
    </b>
    <b>
      <c>text2</c>
   </b>
    <b>
      <c>text3</c>
   </b>
</a>
</root>

當(dāng)前代碼:

import xml.etree.ElementTree as ET
tree = ET.parse('test.xml')
root = tree.getroot()


for x in root.iter():
    if (x.tag == 'a'):
        ET.SubElement(x, 'b')
        ET.SubElement(x, 'c')
        #add text

這似乎有效,除了 'c' 附加為 'a' 而不是 'b' 的子級(jí)

This seems to work except 'c' appends as a child of 'a' rather then 'b'

像這樣:

<root>
<a>
    <b>
      <c>test1</c>
    </b>
    <b>
      <c>test2</c>
    </b>
  <b /><c/></a>
</root>

另外,如何向新創(chuàng)建的元素c"添加文本?我可以遍歷,直到找到?jīng)]有文本但必須有更好的方法的標(biāo)簽c".

Also, how do I add text to the newly created element 'c'? I could iterate through until I find the a tag 'c' that has no text but there must be a better way.

推薦答案

需要指定b作為c的父元素.

You need to specify b as a parent element for c.

此外,為了獲取 a 標(biāo)記,您不需要循環(huán) - 只需獲取根 (a).

Also, for getting the a tag you don't need a loop - just take the root (a).

import xml.etree.ElementTree as ET

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

a = root.find('a')
b = ET.SubElement(a, 'b')
c = ET.SubElement(b, 'c')
c.text = 'text3'

print ET.tostring(root)

打印:

<root>
    <a>
        <b>
          <c>text1</c>
        </b>
        <b>
          <c>text2</c>
        </b>
        <b>
          <c>text3</c>
        </b>
    </a>
</root>

這篇關(guān)于python xml.etree.ElementTree 附加到子元素的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Troubles while parsing with python very large xml file(使用 python 解析非常大的 xml 文件時(shí)出現(xiàn)問(wèn)題)
Find all nodes by attribute in XML using Python 2(使用 Python 2 在 XML 中按屬性查找所有節(jié)點(diǎn))
Python - How to parse xml response and store a elements value in a variable?(Python - 如何解析 xml 響應(yīng)并將元素值存儲(chǔ)在變量中?)
How to get XML tag value in Python(如何在 Python 中獲取 XML 標(biāo)記值)
How to correctly parse utf-8 xml with ElementTree?(如何使用 ElementTree 正確解析 utf-8 xml?)
Parse XML from URL into python object(將 XML 從 URL 解析為 python 對(duì)象)
主站蜘蛛池模板: 在线播放国产一区二区三区 | 黑人中文字幕一区二区三区 | 99成人| 成人精品国产一区二区4080 | 成人亚洲性情网站www在线观看 | 97视频在线免费 | 91在线看片 | 亚洲欧美激情精品一区二区 | 91xx在线观看 | 日韩精品一区二区三区中文在线 | 中文字幕视频在线看5 | 超碰在线免费av | 久久久国产一区二区三区四区小说 | 日日操操| 久久久123 | 亚洲精品乱码久久久久久按摩 | 欧美成人a∨高清免费观看 色999日韩 | 亚洲黄色片免费观看 | 狠狠操网站 | 久久久久国产精品一区三寸 | 久久婷婷色 | 成人av一区二区在线观看 | 欧美国产视频 | 亚洲国产精品久久久 | 黄色大片在线免费观看 | 中文字幕久久久 | 日日综合| 国产91在线 | 欧美 | 九七午夜剧场福利写真 | 欧美成视频在线观看 | 国产激情综合五月久久 | 国产色99| 男女网站在线观看 | 中文字幕高清av | 在线观看成人小视频 | av一级久久 | 国产高潮好爽受不了了夜色 | 日韩免 | 亚洲国产成人av好男人在线观看 | 美女福利视频一区 | 亚洲www|