使用 python 在特定元素前后插入 xml 元素
Insert xml element before and after specific elements using python
我必须在 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
尝试以下(不需要外部库)
import xml.etree.ElementTree as ET
task1 = '''<task Alias="" Lnr="2" Object="CC_JOB"> many more elements_1 </task>'''
task2 = '''<task Alias="" Lnr="x" Object="DC_JOB"> many more elements_2 </task>'''
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>'''
#
# CC_JOB need to be inserted after START and DC_JOB need to be inserted before END.
#
root = ET.fromstring(xml)
task1XML = ET.fromstring(task1)
task2XML = ET.fromstring(task2)
job_struct = root.find('.//JobpStruct')
job_struct.insert(0,task1XML)
job_struct.insert(-2,task2XML)
ET.dump(root)
输出
<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="2" Object="CC_JOB"> many more elements_1 </task><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="x" Object="DC_JOB"> many more elements_2 </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>
我必须在 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
尝试以下(不需要外部库)
import xml.etree.ElementTree as ET
task1 = '''<task Alias="" Lnr="2" Object="CC_JOB"> many more elements_1 </task>'''
task2 = '''<task Alias="" Lnr="x" Object="DC_JOB"> many more elements_2 </task>'''
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>'''
#
# CC_JOB need to be inserted after START and DC_JOB need to be inserted before END.
#
root = ET.fromstring(xml)
task1XML = ET.fromstring(task1)
task2XML = ET.fromstring(task2)
job_struct = root.find('.//JobpStruct')
job_struct.insert(0,task1XML)
job_struct.insert(-2,task2XML)
ET.dump(root)
输出
<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="2" Object="CC_JOB"> many more elements_1 </task><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="x" Object="DC_JOB"> many more elements_2 </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>