私はPythonとXMLで新しく、以下のpythonコードとxmlファイルを使用してxmlファイルの変数の値を置き換え、別の出力xmlファイルを生成します。パラメータ{{param}}
。get_templateを使用した動的なXMLテンプレートの生成
netconf_payload.py
#!/usr/bin/env python
import os
from jinja2 import Environment, FileSystemLoader
PATH = os.path.dirname(os.path.abspath(__file__))
TEMPLATE_ENVIRONMENT = Environment(
autoescape=False,
loader=FileSystemLoader(os.path.join(PATH, 'templates')),
trim_blocks=False)
def render_template(template_filename, context):
return TEMPLATE_ENVIRONMENT.get_template(template_filename).render(context)
def create_index_html():
fname = "bridge.xml"
param = [3, 'ieee', 3]
context = {
'param': param
}
#
with open(fname, 'w') as f:
html = render_template('bridge.xml', context)
f.write(html)
def main():
create_index_html()
if __name__ == "__main__":
main()
テンプレート/ bridge.xml
<vr>
<vrId>0</vrId>
<bridge>
{% set counter = 0 -%}
{% for param in param -%}
<bridgeId>{{ param }}</bridgeId>
<bridgeType>{{ param }}</bridgeType>
<bridgeId>{{ param }}</bridgeId>
{% set counter = counter + 1 -%}
{% endfor -%}
</bridge>
</vr>
は今のコマンドを実行します。のpython netconf_payload.py それは出力ブリッジが生成されます。 xmlファイルは次のとおりです。
<vr>
<vrId>0</vrId>
<bridge>
<bridgeId>3</bridgeId>
<bridgeType>3</bridgeType>
<bridgeId>3</bridgeId>
<bridgeId>ieee</bridgeId>
<bridgeType>ieee</bridgeType>
<bridgeId>ieee</bridgeId>
<bridgeId>3</bridgeId>
<bridgeType>3</bridgeType>
<bridgeId>3</bridgeId>
</bridge>
</vr>
私が欲しい予想される出力は次のようになります。
<vr>
<vrId>0</vrId>
<bridge>
<bridgeId>3</bridgeId>
<bridgeType>ieee</bridgeType>
<bridgeId>3</bridgeId>
</bridge>
</vr>
おかげでスティーブ...その作業 – Rajendra