我必須在 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
uj5u.com熱心網友回復:
嘗試以下(不需要外部庫)
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>
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/442379.html
上一篇:如何填充特定的矢量/svg影像?
