2016-10-07 13 views
0

私はPythonとツリーにはまったく新しく、いくつかの問題が発生しました。Etreeは属性名の代わりに "ランダムな"文字列を返します

私は、次のように構成されたデータセットがあります。私は、属性に到達したいとルートノード要素の両方の属性値を

<?xml version="1.0" encoding="UTF-8"?> 
<graphml xmlns="http://graphml.graphdrawing.org/xmlns"> 
    <node id="someNode"> 
    <data key="label">someNode</data> 
    </node> 
</graphml> 

を。

私はこのようなPythonのxml.etree.ElementTreeを使用してみましたが

import xml.etree.ElementTree as etree 

tree = etree.parse('myDataset') 
root = tree.getroot() 

print('Root: ', root) 

print('Children: ', root.getchildren()) 

が、これは私が得るものです:

Root: <Element '{http://graphml.graphdrawing.org/xmlns}graphml' at 0x031DB5A0> 
Children: [<Element '{http://graphml.graphdrawing.org/xmlns}key' at 0x03F9BFC0> 

私もこれだけ、.textセクションと.TAGを試してみました"at 0x03 ..."を削除しました。

質問が分かりやすく、誰かが解決策を知りたいと思っています。

+1

「ランダムな文字列」ではなく、「オブジェクト」です。 –

+0

何が問題になっているのか分かりません。ここではすべて正常です。 – polku

答えて

1

あなたが出力したい場合は、あなたのルートと子供がXMLテキストとしてノード、代わりにデフォルトの表現の、xml.etree.ElementTree.tostring(root)を使用して

for child in root: 
    xml.etree.ElementTree.tostring(child) 

ここに公式ドキュメント:https://docs.python.org/2/library/xml.etree.elementtree.html#xml.etree.ElementTree.tostring

そして、あなたは彼が名前をタグ付けしたい場合

print(root.tag) 
for child in root: 
    print(child.tag) 

ドキュメント利用可能な属性を記述する:、各要素のtagプロパティを使用https://docs.python.org/2/library/xml.etree.elementtree.html#xml.etree.ElementTree.Element

関連する問題