用 python 3 修改 mule XML 文件
Modify mule XML file with python 3
我正在开发一个简单的自动化 python3 脚本来修改我在 mule4 项目上的一些 xml 文件,并且根据我想添加全局元素的输入,我发现了一些很酷的东西通过使用元素树来完成,但我有一个阻止程序,我无法在其中添加多个属性,作为示例,我有:
<mule>
some_code
</mule>
并且我想将此元素添加为标签内的新子元素 'mule':
<mule>
some_code
<http:listener-config name="HTTPS_Listener_config" doc:name="HTTP Listener config" doc:description="Secure HTTPS listener global config">
<http:listener-connection protocol="HTTPS" host="${https.host}" port="${https.port}" tlsContext="TLS_Context" />
</http:listener-config>
</mule>
所以..除了我testing/playing用过的代码之外,我没有太多实际代码:
import xml.etree.ElementTree as ET
doc = ET.parse("./src/main/mule/global.xml")
root_node = doc.getroot()
child = ET.SubElement(root_node, "http:listener-config")
child.set("name","HTTPS_Listener_config")
connection = ET.SubElement(child,"http:listener-connection")
connection.text = "protocol=HTTPS"
tree = ET.ElementTree(root_node)
tree.write("./src/main/mule/global.xml")
这是结果...还没有完全...加上标签上的 'ns#:'..
...
<http:listener-config name="HTTPS_Listener_config"><http:listener-connection>protocol=HTTPS</http:listener-connection></http:listener-config></ns0:mule>
关于简单示例的任何指示都会对我有很大帮助,谢谢!
可能要使用 python XML 功能,您需要正确设置所有正确的命名空间配置。
虽然更容易出错,但您可能只想 replace text。例如用多行字符串替换结束标记 </mule>
。像这样:
f = open(filein,'r')
filedata = f.read()
f.close()
myconfig=""" <http:listener-config name="HTTPS_Listener_config" doc:name="HTTP Listener config" doc:description="Secure HTTPS listener global config">
<http:listener-connection protocol="HTTPS" host="${https.host}" port="${https.port}" tlsContext="TLS_Context" />
</http:listener-config>
</mule>
"""
newdata = filedata.replace("</mule>",myconfig)
f = open(fileout,'w')
f.write(newdata)
f.close()
我正在开发一个简单的自动化 python3 脚本来修改我在 mule4 项目上的一些 xml 文件,并且根据我想添加全局元素的输入,我发现了一些很酷的东西通过使用元素树来完成,但我有一个阻止程序,我无法在其中添加多个属性,作为示例,我有:
<mule>
some_code
</mule>
并且我想将此元素添加为标签内的新子元素 'mule':
<mule>
some_code
<http:listener-config name="HTTPS_Listener_config" doc:name="HTTP Listener config" doc:description="Secure HTTPS listener global config">
<http:listener-connection protocol="HTTPS" host="${https.host}" port="${https.port}" tlsContext="TLS_Context" />
</http:listener-config>
</mule>
所以..除了我testing/playing用过的代码之外,我没有太多实际代码:
import xml.etree.ElementTree as ET
doc = ET.parse("./src/main/mule/global.xml")
root_node = doc.getroot()
child = ET.SubElement(root_node, "http:listener-config")
child.set("name","HTTPS_Listener_config")
connection = ET.SubElement(child,"http:listener-connection")
connection.text = "protocol=HTTPS"
tree = ET.ElementTree(root_node)
tree.write("./src/main/mule/global.xml")
这是结果...还没有完全...加上标签上的 'ns#:'..
...
<http:listener-config name="HTTPS_Listener_config"><http:listener-connection>protocol=HTTPS</http:listener-connection></http:listener-config></ns0:mule>
关于简单示例的任何指示都会对我有很大帮助,谢谢!
可能要使用 python XML 功能,您需要正确设置所有正确的命名空间配置。
虽然更容易出错,但您可能只想 replace text。例如用多行字符串替换结束标记 </mule>
。像这样:
f = open(filein,'r')
filedata = f.read()
f.close()
myconfig=""" <http:listener-config name="HTTPS_Listener_config" doc:name="HTTP Listener config" doc:description="Secure HTTPS listener global config">
<http:listener-connection protocol="HTTPS" host="${https.host}" port="${https.port}" tlsContext="TLS_Context" />
</http:listener-config>
</mule>
"""
newdata = filedata.replace("</mule>",myconfig)
f = open(fileout,'w')
f.write(newdata)
f.close()