如何将文件中的文本插入到新的XML标记中

2024-04-29 16:04:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下代码来尝试解析一个XML文件,以便它从外部文本文件(如果找到的话)中读取,并将其内容插入到新引入的标记中,并使用结果操作保存一个新的XML文件。在

代码如下:

try:
    import xml.etree.cElementTree as ET
except ImportError:
    import xml.etree.ElementTree as ET
import os

# define our data file
data_file = 'test2_of_2016-09-19.xml'

tree = ET.ElementTree(file=data_file)
root = tree.getroot()

for element in root:
    if element.find('File_directory') is not None:
        directory = element.find('File_directory').text
    if element.find('Introduction') is not None:
        introduction = element.find('Introduction').text
    if element.find('Directions') is not None:
        directions = element.find('Directions').text

for element in root:
    if element.find('File_directory') is not None:
        if element.find('Introduction') is not None:
            intro_tree = directory+introduction
            with open(intro_tree, 'r') as f:
                intro_text = f.read()
            f.closed
            intro_body = ET.SubElement(element,'Introduction_Body')
            intro_body.text = intro_text
        if element.find('Directions') is not None:
            directions_tree = directory+directions
            with open(directions_tree, 'r') as f:
                directions_text = f.read()
            f.closed
            directions_body = ET.SubElement(element,'Directions_Body')
            directions_body.text = directions_text

tree.write('new_' + data_file)

问题是,似乎最后找到的file_directory、introduction和directions的实例被保存并分散到多个条目中,这是不需要的,因为每个条目都有自己的单独记录。在

源XML文件如下所示:

^{pr2}$

所需的输出XML应该如下所示:

<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <Row>
        <Entry_No>1</Entry_No>
        <Waterfall_Name>Bridalveil Fall</Waterfall_Name>
        <File_directory>./waterfall_writeups/1_Bridalveil_Fall/</File_directory>
        <Introduction>introduction-bridalveil-fall.html</Introduction>
        <Directions>directions-bridalveil-fall.html</Directions>
        <Introduction_Body>Text from ./waterfall_writeups/1_Bridalveil_Fall/introduction-bridalveil-fall.html</Introduction_Body>
        <Directions_Body>Text from ./waterfall_writeups/1_Bridalveil_Fall/directions-bridalveil-fall.html</Directions_Body>
    </Row>
    <Row>
        <Entry_No>52</Entry_No>
        <Waterfall_Name>Switzer Falls</Waterfall_Name>
        <File_directory>./waterfall_writeups/52_Switzer_Falls/</File_directory>
        <Introduction>introduction-switzer-falls.html</Introduction>
        <Directions>directions-switzer-falls.html</Directions>
        <Introduction_Body>Text from ./waterfall_writeups/52_Switzer_Falls/introduction-switzer-falls.html</Introduction_Body>
        <Directions_Body>Text from ./waterfall_writeups/52_Switzer_Falls/directions-switzer-falls.html</Directions_Body>
    </Row>
</Root>

但我最终得到的是:

<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <Row>
        <Entry_No>1</Entry_No>
        <Waterfall_Name>Bridalveil Fall</Waterfall_Name>
        <File_directory>./waterfall_writeups/1_Bridalveil_Fall/</File_directory>
        <Introduction>introduction-bridalveil-fall.html</Introduction>
        <Directions>directions-bridalveil-fall.html</Directions>
        <Introduction_Body>Text from ./waterfall_writeups/52_Switzer_Falls/introduction-switzer-falls.html</Introduction_Body>
        <Directions_Body>Text from ./waterfall_writeups/52_Switzer_Falls/directions-switzer-falls.html</Directions_Body>
    </Row>
    <Row>
        <Entry_No>52</Entry_No>
        <Waterfall_Name>Switzer Falls</Waterfall_Name>
        <File_directory>./waterfall_writeups/52_Switzer_Falls/</File_directory>
        <Introduction>introduction-switzer-falls.html</Introduction>
        <Directions>directions-switzer-falls.html</Directions>
        <Introduction_Body>Text from ./waterfall_writeups/52_Switzer_Falls/introduction-switzer-falls.html</Introduction_Body>
        <Directions_Body>Text from ./waterfall_writeups/52_Switzer_Falls/directions-switzer-falls.html</Directions_Body>
    </Row>
</Root>

顺便说一句,有没有什么方法可以在不把正文标签全部打印在一行的情况下介绍正文标签的内容(为了可读性)?在


Tags: htmlbodyelementdirectoryfileintroductionwaterfalldirections
1条回答
网友
1楼 · 发布于 2024-04-29 16:04:11

第一个for循环迭代文档的Row元素,在每次迭代中分别为directoryintroduction和{}变量分配新值,最后得到最后出现的Row元素的值。在

我要做的是创建一个字典将标记名映射到文本内容,然后使用该映射动态添加新的子元素。示例(不读取引用的文件):

for row in root:
    elements = {}
    for node in row:
        elements[node.tag] = node.text

    directory = elements['File_directory']

    intro_tree = directory + elements['Introduction']
    intro_body = ET.SubElement(row, 'Introduction_Body')
    intro_body.text = 'Text from %s' % intro_tree

    directions_tree = directory + elements['Directions']
    directions_body = ET.SubElement(row, 'Directions_Body')
    directions_body.text = 'Text from %s' % directions_tree

相关问题 更多 >