Python XML Minidom 在子节点中按标签获取元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17390166/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python XML Minidom Get element by tag in child node
提问by
I am curently working on a IRC Bot and want to retrieve the configuration from an XML file that look like this :
我目前正在使用 IRC Bot,并希望从如下所示的 XML 文件中检索配置:
<server>
<host> HOST1 </host>
<port> 6667 </port>
<channel>
<name> CHANNAME1</name>
</channel>
<channel>
<name> CHANNAME2 </name>
</channel>
</server>
<server>
<host> HOST2 </host>
<port> 6667 </port>
<channel>
<name> CHANNAME3 </name>
</channel>
</server>
And my code look like this :
我的代码如下所示:
doc = minidom.parse(xml)
node = doc.documentElement
servers = doc.getElementsByTagName("server")
for server in servers:
channels = server.getElementsByTagName("channel")
host = server.getElementsByTagName("host")[0].childNodes[0].data
print host
for channel in channels:
NAME = channel.getElementsByTagName("name")[0].childNode[0].data
print NAME
And the output is
输出是
HOST1
CHANNAME1
CHANNAME2
CHANNAME3
HOST2
CHANNAME1
CHANNAME2
CHANNAME3
But all I need is
但我需要的只是
HOST1
CHANNAME1
CHANNAME2
HOST2
CHANNAME3
Is there a way to get all the elements with the tag name "channel" within my node server instead of the whole xml file ?
有没有办法在我的节点服务器中而不是整个 xml 文件中获取所有带有标记名称“频道”的元素?
采纳答案by James Holderness
Your code looks correct as is. You have childNode
when it should be childNodes
in the NAME
assignment, but I'm assuming that is just a typo in your question.
您的代码看起来是正确的。你有childNode
什么时候应该childNodes
在NAME
作业中,但我假设这只是你问题中的一个错字。
Your XML isn't valid though. You need to have some kind of root node wrapping the servers. As it's currently written, I wouldn't expect that to even parse successfully. It should look something like this:
不过,您的 XML 无效。您需要某种根节点来包裹服务器。正如目前所写的那样,我不希望它能够成功解析。它应该是这样的:
<servers>
<server>
<host> HOST1 </host>
<port> 6667 </port>
<channel>
<name> CHANNAME1</name>
</channel>
<channel>
<name> CHANNAME2 </name>
</channel>
</server>
<server>
<host> HOST2 </host>
<port> 6667 </port>
<channel>
<name> CHANNAME3 </name>
</channel>
</server>
</servers>
With that XML, and the code you've provided, I get the exact output you expect.
使用该 XML 和您提供的代码,我得到了您期望的确切输出。
回答by Martijn Pieters
Don't use the minidom. Use the ElementTree APIinstead. It can handle subtree searches much better:
不要使用minidom。请改用ElementTree API。它可以更好地处理子树搜索:
from xml.etree import ElementTree as ET
doc = ET.parse(xmlfile).getroot()
for server in doc.findall('server'):
host = server.find('./host').text
print host
for channel in server.findall('channel'):
name = channel.find('name').text
print name