在xml中查找特定字符,并在python中用该字符替换值

2024-09-28 12:15:54 发布

您现在位置:Python中文网/ 问答频道 /正文

在xml中查找特定字符并在python中用该字符替换值。我想在xml中查找RESPONSE\u WM并用我得到的字符替换。 如何替换特定路径。 我想将我的第一个响应\u WM更改输出到ABCS\u ACME\u操作\u WS\u BS\u WM 第二种反应是将WM更改为ABCS\U ACME\U操作\U WS\U BS\U WM

import xml.etree.ElementTree as ET
data = """<cus:Customizations xmlns:cus="http://www.bea.com/wli/config/customizations" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xt="http://www.bea.com/wli/config/xmltypes">
    <cus:customization xsi:type="cus:EnvValueActionsCustomizationType">
        <cus:description/>
        <cus:owners>
            <xt:owner>
                <xt:type>BusinessService</xt:type>
                <xt:path>ABCS_ACME_OPERATIONS/business/ABCS_ACME_OPERATIONS_WS_BS</xt:path>
            </xt:owner>
        </cus:owners>
        <cus:actions>
            <xt:replace>
                <xt:envValueType>Work Manager</xt:envValueType>
                <xt:value xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema">RESPONSE_WM</xt:value>
            </xt:replace>
        </cus:actions>
    </cus:customization>
        <cus:customization xsi:type="cus:EnvValueActionsCustomizationType">
        <cus:description/>
        <cus:owners>
            <xt:owner>
                <xt:type>BusinessService</xt:type>
                <xt:path>ABCS_DSF_RESOURCE_OPERATIONS/business/ABCS_DSF_CHECK_SIM_CHANGE_DB_BS</xt:path>
            </xt:owner>
        </cus:owners>
        <cus:actions>
            <xt:replace>
                <xt:envValueType>Service Operational Settings</xt:envValueType>
                <xt:value>
                    <oper:operations enabled="true" xmlns:con="http://xmlns.oracle.com/servicebus/business/config" xmlns:oper="http://xmlns.oracle.com/servicebus/business/operations">
                        <oper:throttling/>
                        <oper:sla-alerting enabled="false" level="normal"/>
                        <oper:resultCachingEnabled>true</oper:resultCachingEnabled>
                    </oper:operations>
                </xt:value>
            </xt:replace>
            <xt:replace>
                <xt:envValueType>Service URI Weight</xt:envValueType>
                <xt:location>0</xt:location>
                <xt:value xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema">0</xt:value>
            </xt:replace>
            <xt:replace>
                <xt:envValueType>Service URI</xt:envValueType>
                <xt:location>0</xt:location>
                <xt:value xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema">jca://eis/DB/DSF</xt:value>
            </xt:replace>
            <xt:replace>
                <xt:envValueType>Service URI Table</xt:envValueType>
                <xt:value xsi:type="tran:URITableType" xmlns:tran="http://www.bea.com/wli/sb/transports">
                    <tran:tableElement>
                        <tran:URI>jca://eis/DB/DSF</tran:URI>
                        <tran:weight>0</tran:weight>
                    </tran:tableElement>
                </xt:value>
            </xt:replace>
            <xt:replace>
                <xt:envValueType>Service Retry Count</xt:envValueType>
                <xt:value xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema">0</xt:value>
            </xt:replace>
            <xt:replace>
                <xt:envValueType>Service Retry Iteration Interval</xt:envValueType>
                <xt:value xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema">30</xt:value>
            </xt:replace>
            <xt:replace>
                <xt:envValueType>Work Manager</xt:envValueType>
                <xt:value xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema">RESPONSE_WM</xt:value>
            </xt:replace>
            <xt:replace>
                <xt:envValueType>JCA Always Use WSDL Flag</xt:envValueType>
                <xt:value xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema">true</xt:value>
            </xt:replace>
            <xt:replace>
                <xt:envValueType>JCA Connection Mode</xt:envValueType>
                <xt:value xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema">managed</xt:value>
            </xt:replace>
            <xt:replace>
                <xt:envValueType>JCA Overwrite Connection Authentication Flag</xt:envValueType>
                <xt:value xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema">false</xt:value>
            </xt:replace>
        </cus:actions>
    </cus:customization>
</cus:Customizations>
"""
tree = ET.fromstring(data)
namespaces = {'cus': 'http://www.bea.com/wli/config/customizations','xt': 'http://www.bea.com/wli/config/xmltypes'}
customizations = ET.fromstring(data)
strings = []
strings2 = []
counter = 0
for A in customizations.findall("cus:customization/cus:actions/xt:replace/[xt:envValueType='Work Manager'][xt:value='RESPONSE_WM']/.../.../cus:owners/xt:owner/[xt:type='BusinessService']/xt:path",namespaces):
        strings.append(A.text)
C = 0
for servicename in strings:
    servicename=strings[C]
    C = C+1
    D=(servicename.split("/business/", 1)[1] + '_WM')
    strings2.append(D)
print(strings2)


from lxml import etree
doc = etree.XML(data)
F = 0
ns = {"xt":"http://www.bea.com/wli/config/xmltypes"}
for new_str in strings2:
   
   new_str = strings2[F]
   print(new_str)
   target = doc.xpath('//xt:owner[xt:type="BusinessService"]//xt:replace[xt:envValueType="Work Manager"]/xt:value',namespaces=ns)
   target[0].text=new_str
   F=F+1
print(etree.tostring(doc).decode())

我得到的错误如下

Traceback (most recent call last): File "wm_replace4.py", line 140, in <module> target[F].text=new_str IndexError: list index out of range


Tags: orghttpvaluewwwtypereplacexmlschemaxt
1条回答
网友
1楼 · 发布于 2024-09-28 12:15:54

有两种方法可以解决这个问题——这里有一种。因为这个问题也被标记为“lxml”,所以我用它代替了elementtree:

from lxml import etree
doc = etree.XML(data)

new_str = "CHANGE_DB_WM"
ns = {"xt":"http://****"}

target = doc.xpath('//xt:replace/xt:value',namespaces=ns)
target[0].text=new_str
print(etree.tostring(doc).decode())

输出是您的预期输出

相关问题 更多 >

    热门问题