使用python在特定元素之前和之后插入xml元素
我必须在 xml 文件中的特定位置插入两个 xml 元素。下面给出了带有任务标签的预定义 xml 元素。我需要在两个位置插入。
<task Alias="" Lnr="2" Object="CC_JOB"> many more elements </task>
<task Alias="" Lnr="x" Object="DC_JOB"> many more elements </task>
CC_JOB
需要插入到 START 之后,DC_JOB
需要插入到 END 之前。
原始 xml 文件:
<?Xml version='1.0' encoding'utf-8'?>
<uc-export clientvers="12.3.7+build.79r8293r8290">
<JOBP AllowExternal="1" name="JOBP_LOAD_CAT_TXT">
<JOBP state="1">
<JobpStruct mode="design">
<task Alias="" Lnr="1" Object="START">
<many_tags many_attributes="many_values"/>
</task>
<task Alias="" Lnr="2" Object="JOB_ONE">
<many_tags many_attributes="many_values"/>
</task>
<task Alias="" Lnr="3" Object="JOB_TWO">
<many_tags many_attributes="many_values"/>
</task>
<task Alias="" Lnr="4" Object="END">
<many_tags many_attributes="many_values"/>
</task>
</JobpStruct>
</JOBP>
</JOBP>
</uc-export>
我的想法是搜索 START 对象并将其与我需要插入的元素连接起来。类似地,对于 END 对象,将插入元素附加到 END 对象。我尝试过 .text、.attrib 等,但还没有成功。
import os
form xml.etree import ElementTree as et
def insert_xml_elements(path_of_xml_file):
for paths, directory, files in os.walk(paths):
for file in files:
if file.endswith("xml"):
filepath = os.path.join(paths, file)
xmlstring = open(filepath, 'r').read()
tree = et.fromstring(xmlstring)
for elem in tree.findall('.//task'):
if elem.attrib.get('Object' == 'START':
elem.txt = et.tostring(elem, encoding='utf8', method = 'xml') + cc_job_element_string
I have to insert two xml element at specific position in a xml file. The predefined xml elements with task tags are given below. That I need to insert at two locations.
<task Alias="" Lnr="2" Object="CC_JOB"> many more elements </task>
<task Alias="" Lnr="x" Object="DC_JOB"> many more elements </task>
CC_JOB
need to be inserted after START and DC_JOB
need to be inserted before END.
Origin xml file:
<?Xml version='1.0' encoding'utf-8'?>
<uc-export clientvers="12.3.7+build.79r8293r8290">
<JOBP AllowExternal="1" name="JOBP_LOAD_CAT_TXT">
<JOBP state="1">
<JobpStruct mode="design">
<task Alias="" Lnr="1" Object="START">
<many_tags many_attributes="many_values"/>
</task>
<task Alias="" Lnr="2" Object="JOB_ONE">
<many_tags many_attributes="many_values"/>
</task>
<task Alias="" Lnr="3" Object="JOB_TWO">
<many_tags many_attributes="many_values"/>
</task>
<task Alias="" Lnr="4" Object="END">
<many_tags many_attributes="many_values"/>
</task>
</JobpStruct>
</JOBP>
</JOBP>
</uc-export>
My Idea is to search for START object and concat it with element that I need to insert. Similarly for END object append inserting element to END object. I have tried .text, .attrib etc but not luck yet.
import os
form xml.etree import ElementTree as et
def insert_xml_elements(path_of_xml_file):
for paths, directory, files in os.walk(paths):
for file in files:
if file.endswith("xml"):
filepath = os.path.join(paths, file)
xmlstring = open(filepath, 'r').read()
tree = et.fromstring(xmlstring)
for elem in tree.findall('.//task'):
if elem.attrib.get('Object' == 'START':
elem.txt = et.tostring(elem, encoding='utf8', method = 'xml') + cc_job_element_string
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
(不需要外部库)
尝试下面的输出
Try to below (no external lib required)
output