久久久久久久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ì)象)
主站蜘蛛池模板: 日韩av在线一区 | 亚洲欧洲日本国产 | 精品久久香蕉国产线看观看亚洲 | 波多野结衣在线观看一区二区三区 | 久久最新 | 精品久久久久久亚洲综合网 | 精品99爱视频在线观看 | 亚洲福利在线观看 | 国产美女高潮 | 日韩一二区 | 日韩和的一区二区 | www成人免费| 日韩精品一区二区三区视频播放 | 无码日韩精品一区二区免费 | 91麻豆精品一区二区三区 | 国家aaa的一级看片 h片在线看 | 亚洲网站在线观看 | 一区二区国产精品 | 欧美成人一区二区 | 成人免费在线播放视频 | 日韩av一区在线观看 | 久久99久久98精品免观看软件 | 欧美激情啪啪 | 免费激情 | 色www精品视频在线观看 | 欧美日韩国产高清 | 天天操夜夜操 | 日本不卡免费新一二三区 | 北条麻妃99精品青青久久主播 | 免费久久99精品国产婷婷六月 | 欧美一级欧美一级在线播放 | 一区二区三区不卡视频 | 中文字幕一区二区三区四区五区 | 国产亚洲欧美在线 | 国产欧美一区二区精品忘忧草 | 伊人久麻豆社区 | 国产精品久久久久无码av | 99色视频| 欧美一区二区三区在线观看 | 中文字幕亚洲一区二区三区 | 欧美舔穴|