久久久久久久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热国产在线播放 | 免费黄色av | 日本在线中文 | 日本在线一区二区三区 | 国产欧美在线一区 | 奇米久久久 | 国产精品久久久久久久久免费软件 | 亚洲欧美国产精品久久 | 日韩三级电影一区二区 | 亚洲精品国产综合区久久久久久久 | 日韩精品久久一区二区三区 | 一区二区三区视频在线 | 欧美视频一区 | 一区二区视频在线 | 欧美成人高清视频 | 手机av在线| 日韩欧美三区 | 欧美在线观看一区 | 国产精品日韩欧美一区二区三区 | 精品成人免费一区二区在线播放 | 亚洲精品视频免费观看 | 亚洲视频免费在线观看 | 男女网站免费 | 国产a区| 青青草这里只有精品 | 国产三区av | 国产精品视频一区二区三区四蜜臂 | 围产精品久久久久久久 | 亚洲视频中文字幕 | 欧美电影网 | 国产 欧美 日韩 一区 | 中文字幕精品视频 | 在线黄av | 亚洲成av|