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

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

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

問題描述

我正在嘗試使用 xml.etree.ElementTree 來解析一個 xml 文件,找到一個特定的標簽,將一個子附加到該標簽,將另一個子附加到新創建的標簽,并將文本添加到后一個子.

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>

當前代碼:

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' 的子級

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>

另外,如何向新創建的元素c"添加文本?我可以遍歷,直到找到沒有文本但必須有更好的方法的標簽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 標記,您不需要循環 - 只需獲取根 (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>

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

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

相關文檔推薦

Troubles while parsing with python very large xml file(使用 python 解析非常大的 xml 文件時出現問題)
Find all nodes by attribute in XML using Python 2(使用 Python 2 在 XML 中按屬性查找所有節點)
Python - How to parse xml response and store a elements value in a variable?(Python - 如何解析 xml 響應并將元素值存儲在變量中?)
How to get XML tag value in Python(如何在 Python 中獲取 XML 標記值)
How to correctly parse utf-8 xml with ElementTree?(如何使用 ElementTree 正確解析 utf-8 xml?)
Parse XML from URL into python object(將 XML 從 URL 解析為 python 對象)
主站蜘蛛池模板: 黄色国产视频 | 一区二区三区四区免费视频 | 黄色三级视频在线观看 | 一级片在线观看视频 | 日韩999 | 国产一区二区三区视频在线 | 午夜一级片 | 日韩成人精品 | www.久久久久 | 成人免费视频国产免费 | 一区在线播放 | 亚洲国产毛片 | 亚洲va韩国va欧美va精品 | 久久中文网| 五月天激情综合 | 日韩免费成人 | 香蕉视频一区二区 | 国产精品一区二区三区免费 | 日韩欧美黄色片 | 麻豆av在线播放 | 欧美激情视频网站 | 成人国产精品一区二区 | 国产成人综合在线 | 日日操天天操 | 国产一区二区视频在线观看 | 国产精品一级 | 日韩一二三 | 欧美一区二 | 久草福利在线 | 中文字幕在线观看亚洲 | 国产精品一级 | 国产伦精品一区二区三区免.费 | 国产suv一区二区 | 国产无遮挡又黄又爽免费网站 | 一区二区三区久久久 | 日韩免费在线观看 | 日韩免费视频 | 成人深夜福利视频 | 日韩欧美在线观看视频 | 福利视频网站 | 日日夜夜精品免费 |