Search code examples
pythonxmlpython-2.7aiml

Xml written twice while appending


When trying to append an existing xml file using xml.etree.ElementTree. The sub elements are written twice. Is there any way we can overcome this.

This is my current code:

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 before written:

<?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 after written:

  <?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>


Solution

  • you xml is throwing errors on parsing , so i would go with an example here.

    Parse your data

    root= ET.XML(filename)
    

    do your modifications whatever you want (adding nodes,values) e.g. from the python document

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

    all the changes are saved to root element.

    Write it to a xml file

    you need to pass root to ET.ElementTree(), that will save all your previous changes and would write to xml

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