雑多な技術系メモ

自分用のメモ。内容は保証しません。よろしくお願いします。

xml

pythonのElementTree(xmlファイル)で編集して、きれいに(改行をうまくして)保存する方法

サンプルコード from xml.dom import minidom 省略 xmlstr = minidom.parseString(ET.tostring(root)).toprettyxml(indent=" ") with open("xml.xml", "w") as f: f.write(xmlstr) 参考 https://stackoverflow.com/questions/28813876/how-do-i-get-pythons-…

【python】【xml】すでにあるxmlに要素を追加

サンプルコード import xml.etree.ElementTree as ET tree = ET.ElementTree(file="file.xml") # xmlを読み込む root = tree.getroot() element = ET.Element("vType") # 要素の作成 element.set("A", "a") # 属性の追加 root.insert(0, element) # 作成した…