9
ElementTree
のソースをPythonの文字列としてどのように取得できますか?Python xmlの文字列ソースを取得ElementTree
ElementTree
のソースをPythonの文字列としてどのように取得できますか?Python xmlの文字列ソースを取得ElementTree
import xml.etree.ElementTree as ET
tree = ET.parse(source)
root = tree.getroot()
ET.tostring(root)
source
とET.tostring(doc)
の内容の違いがフォーマットされてもよいことに留意されたいです。
'ElementTree'オブジェクトには属性がありません。これを試したときに 'tostring'という属性が発生します。 – James
@James:間違ったオブジェクトで呼び出しています。その方法を持つのは* module *です。 –
@James: 'tree'が' ElementTree'(私の間違い)であれば 'ET.tostring(tree)'はうまくいきません。代わりに、 'root = tree.getroot()'でツリーのルートを取得し、 'ET.tostring(root)'を呼び出します。 – unutbu