Python 从包含xml的变量中获取值
我有下一个jenkins API脚本:Python 从包含xml的变量中获取值,python,xml,jenkins,Python,Xml,Jenkins,我有下一个jenkins API脚本: import jenkins import json import re server = jenkins.Jenkins('https://jenkins_url', username, password) nodes = json.dumps(server.get_nodes()) nodes = re.sub('\"offline\"|[:{} \[\]]|true,|false,|\"name\"|\"','',nodes).split(',')
import jenkins
import json
import re
server = jenkins.Jenkins('https://jenkins_url', username, password)
nodes = json.dumps(server.get_nodes())
nodes = re.sub('\"offline\"|[:{} \[\]]|true,|false,|\"name\"|\"','',nodes).split(',')
for label in nodes:
if label != 'master':
print label
node_config = server.get_node_config(label)
print node_config
节点配置包含下一个xml文本,例如:
<?xml version="1.0" encoding="UTF-8"?>
<slave>
<name>test.server</name>
<description></description>
<remoteFS>/var/lib/jenkins</remoteFS>
<numExecutors>1</numExecutors>
<mode>EXCLUSIVE</mode>
<retentionStrategy class="hudson.slaves.RetentionStrategy$Always"/>
<launcher class="hudson.plugins.sshslaves.SSHLauncher" plugin="ssh-slaves@1.10">
<host>test.server</host>
<port>7777</port>
<credentialsId>d0970a8f-d124</credentialsId>
<maxNumRetries>0</maxNumRetries>
<retryWaitTime>0</retryWaitTime>
</launcher>
<label>BuildServer</label>
<nodeProperties/>
<userId>test</userId>
</slave>
测试服务器
/var/lib/jenkins
1.
独家
测试服务器
7777
d0970a8f-d124
0
0
构建服务器
测试
我想获得每个标签的值,以便在输出上获得,例如test.server等
你能帮我一下吗?xml\u text=“”
xml_text = """<?xml version="1.0" encoding="UTF-8"?>
<slave>
<name>test.server</name>
<description></description>
<remoteFS>/var/lib/jenkins</remoteFS>
<numExecutors>1</numExecutors>
<mode>EXCLUSIVE</mode>
<retentionStrategy class="hudson.slaves.RetentionStrategy$Always"/>
<launcher class="hudson.plugins.sshslaves.SSHLauncher" plugin="ssh-slaves@1.10">
<host>test.server</host>
<port>7777</port>
<credentialsId>d0970a8f-d124</credentialsId>
<maxNumRetries>0</maxNumRetries>
<retryWaitTime>0</retryWaitTime>
</launcher>
<label>BuildServer</label>
<nodeProperties/>
<userId>test</userId>
</slave>
"""
import xml.etree.ElementTree
root = xml.etree.ElementTree.fromstring(xml_text)
# show only a particular tag
for name in root.findall('name'):
print(name.text)
# show all children at first level
for child in root:
print('%s: %s' % (child.tag, child.text))
# build a dict (will only get last of any duplicate tags, and no children)
slave = {child.tag: child.text for child in root}
# build a dict (will only get last of any duplicate tags)
def xml_todict(xml_node):
dict_ = {}
for child in xml_node:
dict_[child.tag] = xml_todict(child)
dict_.update(xml_node.attrib)
if not dict_:
return xml_node.text
if xml_node.text and 'text' not in dict_:
dict_['text'] = xml_node.text
return dict_
slave = xml_todict(root)
测试服务器
/var/lib/jenkins
1.
独家
测试服务器
7777
d0970a8f-d124
0
0
构建服务器
测试
"""
导入xml.etree.ElementTree
root=xml.etree.ElementTree.fromstring(xml\u文本)
#仅显示特定的标记
对于root.findall('name')中的名称:
打印(name.text)
#在第一级显示所有子级
对于根目录中的子目录:
打印(“%s:%s%”(child.tag,child.text))
#构建dict(将只获取所有重复标记中的最后一个,并且没有子标记)
slave={child.tag:child.text用于根目录中的子目录}
#构建dict(将只获取所有重复标记中的最后一个)
def xml_todict(xml_节点):
dict_uu={}
对于xml_节点中的子节点:
dict_u[child.tag]=xml_todict(child)
dict.update(xml\u node.attrib)
如果没有记录:
返回xml_node.text
如果xml_node.text和“text”不在dict_中:
dict_u['text']=xml_node.text
返回指令_
slave=xml\u todict(根)
它会打印除以下内容之外的所有内容:test.server 7777 d0970a8f-d124 0 0我已经发现错误:我必须将root.findall('name')改为root.findall('../host')。根据文档“/”选择当前元素下所有级别上的所有子元素(搜索整个子树)。例如,“.//egg”选择整个树中的所有“egg”元素等。感谢您的帮助!