我不熟悉python和xml。我正在使用python生成一个xml文件。这个xml文件将每天根据一些输入配置文件生成。 在xml文件中创建子元素时需要一些帮助。因为它不是在正确的父元素下生成的。请输入当前和期望的输出。你知道吗
我有一个包装器python脚本和生成器python脚本。 发电机
with workflow.SubWFAction() as a:
inp_list = [('catg','first')]
a.app_path(TEST_FILE)
a.configsec(inp_list)
wf.insert_action(a)
发电机Py sript:
from xml.etree import ElementTree
from xml.etree.ElementTree import Element
from xml.etree.ElementTree import SubElement
from xml.dom import minidom
class Workflow:
def __init__(self, name, namespace=WF_NAMESPACE):
self.xml = Element('workflow-app')
self.action_counter = 0
def insert_action(self, action):
class Action:
def __init__(self):
self.xml = Element('action')
self._position = 0
class SubWFAction(Action):
def __init__(self):
Action.__init__(self)
action_type = 'sub-workflow'
self.xml.set('name', action_type)
this_node = Element(action_type)
self.xml.insert(self._position, this_node)
self.sub_workflow = self.xml[self._position]
def app_path(self, inner_text):
node = SubElement(self.sub_workflow, 'app-path')
cpnode = SubElement(self.sub_workflow, 'propagate-configuration')
node.text = inner_text
def configsec(self, inp_list):
configuration = SubElement(self.sub_workflow, 'configuration')
proptext = [self.property(prop[0], prop[1]) for prop in inp_list]
return proptext
def property(self, key, value):
property = SubElement(self.xml,'property')
name = SubElement(property,'name')
name.text = key
val = SubElement(property,'value')
val.text = value
电流输出:
<?xml version="1.0" encoding="UTF-8"?>
<workflow-app name="wf_ntflx_genr" xmlns="uri:oozie:workflow:0.5">
<start to="sub-workflow"/>
<action name="sub-workflow">
<sub-workflow>
<app-path>
/ntflx/tesfile.dat
</app-path>
<propagate-configuration/>
<configuration/>
</sub-workflow>
<configuration/>
<ok to="end"/>
<error to="kill"/>
<property>
<name>
catg
</name>
<value>
first
</value>
</property>
</action>
期望输出:
<?xml version="1.0" encoding="UTF-8"?>
<workflow-app name="wf_ntflx_genr" xmlns="uri:oozie:workflow:0.5">
<start to="sub-workflow"/>
<action name="sub-workflow">
<sub-workflow>
<app-path>
/ntflx/tesfile.dat
</app-path>
<propagate-configuration/>
<configuration>
<property>
<name>
catg
</name>
<value>
first
</value>
</property>
</configuration>
</sub-workflow>
<ok to="end"/>
<error to="kill"/>
</action>
请帮帮我!你知道吗
您希望
<property>
位于<configuration>
之下,因此需要向SubElement
提供正确的元素。你可以用几种方法来做,但我不确定哪种方法对你的整体设计最有意义。但是一种方法是跟踪当前的配置,并让property
将其数据放到其中。我注意到代码中有一个潜在的错误。。。你知道吗相关问题 更多 >
编程相关推荐