【问题标题】:Xml written twice while appending附加时写入两次 Xml
【发布时间】:2017-12-04 08:05:27
【问题描述】:

尝试使用 xml.etree.ElementTree 附加现有 xml 文件时。子元素被写入两次。有什么办法可以克服。

这是我当前的代码:

with open('filename.aiml',"a+") as f:
                        tree=ET.parse(f)
                        root=tree.getroot()
                        rootTag=root.find('.')
                        category=ET.SubElement(rootTag,'category')
                        pattern=ET.SubElement(category,'pattern')
                        pattern.text=input_text.upper()
                        template=ET.SubElement(category,'template')
                        template.text=response
                        root.append(category)
                        tree.write(open("filename.aiml","w+"),encoding='ISO-8859-1')

写之前的XML:

<?xml version='1.0' encoding='ISO-8859-1'?>
<aiml version="1.0">
<category>
<pattern>WHAT IS DEEP LEARNING</pattern>
<template>Deep learning (also known as deep structured learning or hierarchical learning) is part of a broader family of machine learning methods based on learning data representations, as opposed to task-specific algorithms.</template>
</category>

写入后的XML:

  <?xml version='1.0' encoding='ISO-8859-1'?>
    <aiml version="1.0">
    <category>
    <pattern>WHAT IS DEEP LEARNING</pattern>
    <template>Deep learning (also known as deep structured learning or hierarchical learning) is part of a broader family of machine learning methods based on learning data representations, as opposed to task-specific algorithms.</template>
    </category>
 <category>
<pattern>WHAT IS PROOF OF CONCEPT</pattern>
<template>Proof of Concept (PoC) is a realization of a certain method or idea in order to demonstrate its feasibility.</template>
</category>
<category><pattern>TELL ME ABOUT POC</pattern>
<template>Proof of Concept (PoC) is a realization of a certain method or idea in order to demonstrate its feasibility</template>
</category>
<category><pattern>WHAT IS PROOF OF CONCEPT</pattern>
<template>Proof of Concept (PoC) is a realization of a certain method or idea in order to demonstrate its feasibility</template>
</category>
<category><pattern>TELL ME ABOUT POC</pattern>
<template>Proof of Concept (PoC) is a realization of a certain method or idea in order to demonstrate its feasibility.</template></category>

【问题讨论】:

  • 永远不要使用open() 读取 XML 文件。使用tree = ET.parse('path/to/file')。后者正确处理文件编码,前者没有。后者也更短。话虽如此,您不能“附加”到 XML 文件。加载树、修改树、保存树。
  • @Tomalak 我需要将新元素添加到现有的 XML 文件中。但是当我不使用tree.write(open("filename.aiml","w+"),encoding='ISO-8859-1') 时,它会显示 XML 解析错误
  • 你在看我说的吗?我说你不能追加到 XML 文件
  • 其实上面的代码可以用来追加到现有的xml文件。唯一的问题是子元素被写入了两次。
  • 不,它不能。但显然你知道自己在做什么,所以继续吧。

标签: python xml python-2.7 aiml


【解决方案1】:

你的 xml 在解析时抛出错误,所以我会在这里举个例子。

解析你的数据

root= ET.XML(filename)

随心所欲地进行修改(添加节点、值) 例如来自python文档

for rank in root.iter('rank'):
...     new_rank = int(rank.text) + 1
...     rank.text = str(new_rank)
...     rank.set('updated', 'yes')

所有更改都保存到根元素。

将其写入 xml 文件

您需要将 root 传递给 ET.ElementTree(),这将保存您之前的所有更改并写入 xml

with open('d:\output.xml', 'wb') as file:
    ET.ElementTree(root).write(file, encoding='utf-8', xml_declaration=True)

【讨论】:

    猜你喜欢
    • 2016-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多