2013-05-08 90 views
2
import xml.dom.minidom 

water = """ 
<channel> 
<item> 
<title>water</title> 
<link>http://www.water.com</link> 
</item> 
<item> 
<title>fire</title> 
<link>http://www.fire.com</link> 
</item> 
</channel>""" 

dom=xml.dom.minidom.parseString(water) 
linklist = dom.getElementsByTagName('link') 
print (len(linklist)) 

使用minidom,我想获取链接和/链接之间的内容作为字符串。 请让我知道如何。如何获取python中两个xml标签之间的内容?

回答

2

如果你想坚持xml.dom的.minidom只需调用.firstChild.nodeValue。例如,存储在变量“链表”中的链接,所以打印出来简单地遍历它们并呼吁.firstChild.nodeValue,像这样...

for link in linklist: 
    print link.firstChild.nodeValue 

打印...

http://www.water.com 
http://www.fire.com 

更详细的解答这里.... Get Element value with minidom with Python


在回答您的其他问题:
如果你想得到一个特定的元素,你需要知道它在文档中的位置或搜索它。

例如,如果你知道你想要的是XML文档,你会做的第二个链接的链接...

# the variable fire_link is a DOM Element of the second link in the xml file 
fire_link = linklist[1] 

但是,如果你想要的链接,但不知道它在哪里该文件,你将不得不搜索它。下面是一个例子...

# fire_link is a list where each element is a DOM Element containing the http://www.fire.com link 
fire_links = [l for l in linklist if l.firstChild.nodeValue == 'http://www.fire.com'] 

# take the first element 
fire_link = fire_links[0] 
+0

那么,我该如何获得某个元素?不打印所有 – user2351602 2013-05-08 13:22:42

+0

您需要知道它的位置或所需元素的文字。我会附上一些例子。 – b10hazard 2013-05-08 13:27:45

1

这比看起来更复杂。从文档中的例子,在你的问题把这段代码:

def getText(nodelist): 
    rc = [] 
    for node in nodelist: 
     if node.nodeType == node.TEXT_NODE: 
      rc.append(node.data) 
    return ''.join(rc) 

text = getText(linklist[0].childNodes) 
print text 

我建议尝试the elementtree module其中代码如下:

print linklist[0].text 
+0

+1 ElementTree的(或者,如果需要更多的LXML) – Mark 2013-05-08 13:05:00

+0

回溯(最近最后一次通话): 文件 “C:/Users/lee/Desktop/www.py”第28行,在 text = getText(linklist [0] .childNodes) 文件“C:/Users/lee/Desktop/www.py”,第24行,在getText中 if node.nodetype == node.TEXT_NODE: AttributeError:'文本'对象没有属性'nodetype'我收到一条错误消息。 – user2351602 2013-05-08 13:08:56

相关问题