Сгенерируйте дерево XML со значениями, используя скрипт py - PullRequest
0 голосов
/ 19 марта 2020

Я новичок в python и хотел бы создать дерево XML со значениями.

Я хочу поместить оба файла jsc: //xxx.js "как а также "EXT.F C. XML" в элементе ресурса и политики в XML через код python. Все jsc: //xxx.js "и« EXT.F C. XML "файлы хранятся в моей локальной папке с именами" resources "и" policy ".

Требуемый вывод

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<APIProxy revision="2" name="Retirement_Digital_Account_Balance">
    <ManifestVersion>SHA-512:f9ae03c39bf00f567559e</ManifestVersion>
    <Policies>
        <Policy>EXT.FC_Env_Host</Policy>
        <Policy>EXT.FC_JWTVerf</Policy>
        <Policy>EXT.JSC_Handle_Fault</Policy>
    </Policies>
    <ProxyEndpoints>
        <ProxyEndpoint>default</ProxyEndpoint>
    </ProxyEndpoints>
    <Resources>
        <Resource>jsc://createErrorMessage.js</Resource>
        <Resource>jsc://jwtHdrExt.js</Resource>
        <Resource>jsc://log-variables.js</Resource>
        <Resource>jsc://swagger.json</Resource>
        <Resource>jsc://tgtDataForm.js</Resource>
    </Resources>
</APIProxy>

Я использую дерево элементов для преобразования в xml файл, это код, который я запускаю

import xml.etree.ElementTree as ET
from xml.etree.ElementTree import Element, SubElement, Comment
from xml.etree import ElementTree, cElementTree
from xml.dom import minidom
from ElementTree_pretty import prettify
import datetime
import os

generated_on = str(datetime.datetime.now())
#proxy = Element('APIProxy')
proxy = Element('APIProxy', revision = "2", name = "Retirement_Digital_Account_Balance")

ManifestVersion = SubElement(proxy, 'ManifestVersion')
ManifestVersion.text = 'SHA-512:f9ae03c39bf00f567559e'

Policies = SubElement(proxy, 'Policies')
Policy = SubElement(Policies, 'Policy')

path = '/policies'
#files = []
# r=root, d=directories, f = files
for r, d, f in os.walk(path):
    for file in f:
        if '.xml' in file:
            Policy.append(os.path.join(r, file))

    for p in Policy:
        print(p)

ProxyEndpoints = SubElement(proxy, 'ProxyEndpoints')
ProxyEndpoint = SubElement(ProxyEndpoints, 'ProxyEndpoint')
ProxyEndpoint.text = 'default'

Resources = SubElement(proxy, 'Resources')
Resource = SubElement(Resources, 'Resource')
path = '/Resources'
# r=root, d=directories, f = files
for r, d, f in os.walk(path):
    for file in f:
        if 'js' in file:
            Resource.append(os.path.join(r, file))

    for R in Resource:
        print(R)

Spec = SubElement(proxy, 'Spec')
Spec.text = ""
#proxy.append(Spec)

proxy.append(Element('TargetServers'))

TargetEndpoints = SubElement(proxy, 'TargetEndpoints')
TargetEndpoint = SubElement(TargetEndpoints, 'TargetEndpoint')
TargetEndpoint.text = 'default'

print(ET.tostring(proxy))
tree = cElementTree.ElementTree(proxy) # wrap it in an ElementTree instance, and save as XML

t = minidom.parseString(ElementTree.tostring(proxy)).toprettyxml() # Since ElementTree write() has no pretty printing support, used minidom to beautify the xml.
tree1 = ElementTree.ElementTree(ElementTree.fromstring(t))

tree1.write("Retirement_Digital_Account_Balance_v2.xml",encoding='UTF-8', xml_declaration=True)

Хорошо, код работает, но я не получил желаемый вывод, я получил следующее:

<?xml version='1.0' encoding='UTF-8'?>
<APIProxy name="Retirement_Digital_Account_Balance" revision="2">
    <ManifestVersion>SHA-512:f9ae03c39bf00f567559e</ManifestVersion>
    <Policies>
        <Policy />
    </Policies>
    <ProxyEndpoints>
        <ProxyEndpoint>default</ProxyEndpoint>
    </ProxyEndpoints>
    <Resources>
        <Resource />
    </Resources>
</APIProxy>

Как использовать l oop в ElementTree в python, чтобы импортировать значения из папки и создать дерево XML с его ценности?

...