2011-01-10 60 views
0

我正在迭代xml節點值,它是從xpath查詢中獲得的。我需要爲某個節點值分配一個引用。對lxml節點的引用

在這裏,我想怎麼到:

from lxml import etree 

doc = etree.fromstring(some_xml) 
nodes = doc.xpath('some_query') 
for node in nodes: 
    if node.text == 'smth': 
     #there i need to assign reference 
     reference = node.text 

#after iterating i need to that change the node value 
reference = 'hello world' 

是有辦法做到這一點在Python?

謝謝!

回答

1

店實際節點,而不是一個參考:

from lxml import etree 

doc = etree.fromstring(some_xml) 
nodes = doc.xpath('some_query') 
for node in nodes: 
    if node.text == 'smth': 
     reference = node 

reference.text = 'hello world' 
+0

是引發AttributeError的:「‘NoneType’對象有沒有屬性‘文本’」 – nukl 2011-01-10 23:46:19