python - 使用python替换XML标签值
问题描述
我有一个 XML 文件,我需要在其中替换过滤器标记内的值。我试图解析这个 XML,但看起来有一些我无法解决的错误。任何人都可以帮我更换价值。
path = """
<SOAP:Envelope xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP:Header>
<header xmlns="xmlapi_1.0">
<security>
<user>testuser</user>
<password hashed="false">testpassword</password>
</security>
<requestID>XML_API_client@n</requestID>
</header>
</SOAP:Header>
<SOAP:Body>
<find>
<fullClassName>equipment.PhysicalPort</fullClassName>
<filter>
<equal name="siteId" value="x.x.x.x." />
</filter>
<resultFilter>
<attribute>objectFullName</attribute>
<attribute>displayedName</attribute>
<attribute>portName</attribute>
<attribute>description</attribute>
<attribute>lagMembershipId</attribute>
<attribute>encapType</attribute>
<attribute>mode</attribute>
<attribute>speed</attribute>
<attribute>mtuValue</attribute>
</resultFilter>
</find>
<find xmlns="xmlapi_1.0">
<fullClassName>ethernetequipment.EthernetPortSpecifics</fullClassName>
<filter>
<equal name="siteId" value="x.x.x.x." />
</filter>
<resultFilter>
<attribute>autoNegotiate</attribute>
<attribute>downWhenLooped</attribute>
</resultFilter>
</find>
"""
我尝试的另一种方式:
path = '../request.xml'
IP = '"' + '63.130.111.89' + '"' + '/>'
f = open('test.xml', 'w+')
for line in open(path, 'r'):
output_line = line
string1, string2 = "siteId", "value"
if string1 in output_line:
value = output_line.split("value"'=', 1)[1]
output_line = str.replace(output_line, value, IP)
f.write(output_line)
解决方案
推荐阅读
- ios - 节点中的 AVPlayerViewController
- javascript - 从 HTML 页面上传文件的问题
- clojurescript - Reagent/ClojureScript - 多组件方面更新
- objective-c - 合并数组的值
- python - Python 模拟媒体键
- mongodb - mongodump 在无效的 URL 或无法访问的服务器上永远等待
- ruby-on-rails - 过度使用路由自定义 - customize_count (Rails 5)
- django - Django DRF 视图过滤多对多查询集
- python - 在python中将JSON字典转换为JSON数组
- adobe - Adobe体验云登录