如何使用子子元素更新 xml 中的值

问题描述 投票:0回答:1

我想从给定的 XML 更新下面提到的 2 个子子元素的值。

  1. 语言首选项__GET.jtl
</hashTree>
        <ResultCollector guiclass="ViewResultsFullVisualizer" testclass="ResultCollector" testname="View Results Tree" enabled="false">
          <boolProp name="ResultCollector.error_logging">false</boolProp>
          <objProp>
            <name>saveConfig</name>
            <value class="SampleSaveConfiguration">
              <time>true</time>
              <latency>true</latency>
              <timestamp>true</timestamp>
              <success>true</success>
              <label>true</label>
              <code>true</code>
              <message>true</message>
              <threadName>true</threadName>
              <dataType>false</dataType>
              <encoding>false</encoding>
              <assertions>true</assertions>
              <subresults>false</subresults>
              <responseData>false</responseData>
              <samplerData>false</samplerData>
              <xml>false</xml>
              <fieldNames>true</fieldNames>
              <responseHeaders>false</responseHeaders>
              <requestHeaders>false</requestHeaders>
              <responseDataOnError>true</responseDataOnError>
              <saveAssertionResultsFailureMessage>true</saveAssertionResultsFailureMessage>
              <assertionsResultsToSave>0</assertionsResultsToSave>
              <bytes>true</bytes>
              <hostname>true</hostname>
              <threadCounts>true</threadCounts>
              <sampleCount>true</sampleCount>
            </value>
          </objProp>
          <stringProp name="filename"></stringProp>
        </ResultCollector>
        <hashTree/>
        <ResultCollector guiclass="StatVisualizer" testclass="ResultCollector" testname="Aggregate Report" enabled="true">
          <boolProp name="ResultCollector.error_logging">false</boolProp>
          <objProp>
            <name>saveConfig</name>
            <value class="SampleSaveConfiguration">
              <time>true</time>
              <latency>true</latency>
              <timestamp>true</timestamp>
              <success>true</success>
              <label>true</label>
              <code>true</code>
              <message>true</message>
              <threadName>true</threadName>
              <dataType>true</dataType>
              <encoding>false</encoding>
              <assertions>true</assertions>
              <subresults>false</subresults>
              <responseData>false</responseData>
              <samplerData>false</samplerData>
              <xml>false</xml>
              <fieldNames>true</fieldNames>
              <responseHeaders>false</responseHeaders>
              <requestHeaders>false</requestHeaders>
              <responseDataOnError>true</responseDataOnError>
              <saveAssertionResultsFailureMessage>true</saveAssertionResultsFailureMessage>
              <assertionsResultsToSave>0</assertionsResultsToSave>
              <bytes>true</bytes>
              <sentBytes>true</sentBytes>
              <url>true</url>
              <threadCounts>true</threadCounts>
              <idleTime>true</idleTime>
              <connectTime>true</connectTime>
            </value>
          </objProp>
          <stringProp name="filename">language-preferences__GET.jtl</stringProp>
        </ResultCollector>
        <hashTree/>

我编写了下面的代码来访问

ResultCollector
的子元素。但是,我无法找出浏览多个子子元素的更好方法。

    tree = ET.parse(jmx_path)
    aggregate_samplers = tree.findall('.//ResultCollector[@testname="Aggregate Report"]')
    active_aggregate_samplers = [active_aggregate_sampler for active_aggregate_sampler in aggregate_samplers
                                 if active_aggregate_sampler.attrib['enabled'] == 'true']
    for active_sampler in active_aggregate_samplers:
        for child in active_sampler:
            if child.tag == 'objProp':
                # < update **subresults** value as true>
            elif child.attrib['name'] == 'filename':
                child.text = str(uuid.uuid4()) + '_' + child.text // new file name
                # < update **filename** value as c82fa39b-5079-4838-b17c-fdf85f49fd5f_language-preferences__GET.jtl>
python xml elementtree
1个回答
0
投票

注意: 您的示例 xml 片段没有根元素!

答案: 如果 xml 已更正,您可以使用

findall()
找到您感兴趣的标签列表。然后更改相关列表元素的文本内容:

import xml.etree.ElementTree as ET

tree = ET.parse("hashTree.xml")
root = tree.getroot()

subres_list = root.findall(".//subresults")
for sub in subres_list:
    sub.text = "true" # your new text for the list elements

stringP_list = root.findall(".//stringProp")
for stringP in stringP_list:
    stringP.text = "c82fa39b-5079-4838-b17c-fdf85f49fd5f_language-preferences__GET.jt"
© www.soinside.com 2019 - 2024. All rights reserved.