Home > Mobile >  create a tag at top of xml file using python
create a tag at top of xml file using python

Time:11-21

I have xml file named 'test.xml' like below format

<final_output>
    <career_profile>
        <output>
            <Template OriginalSentence="1" SentenceID="1" RecordID="0">
                <Employer Type="String"><Value>HCL TECHNOLOGY LTD</Value></Employer>
                <duration><Value>JAN 2018 to till date</Value></duration>
            </Template>
        </output>
    </career_profile>
</final_output>

I want to add a final_file tag on top of test.xml file using python.

The output should look like:

<final_file>
    <final_output>
        <career_profile>
            <output>
                <Template OriginalSentence="1" SentenceID="1" RecordID="0">
                    <Employer Type="String"><Value>HCL TECHNOLOGY LTD</Value></Employer>
                    <duration><Value>JAN 2018 to till date</Value></duration>
                </Template>
            </output>
        </career_profile>
    </final_output>
</final_file>

For this I have used additional xml file named 'random.xml' file containing <final_file> </final_file> .

random.xml looks like:

<final_file>

</final_file>

I am not getting the <final_output> </final_output> tag in the resultant output.xml.

I have tried the following code:

import xml.etree.ElementTree as ET

xmlfile1 = "random.xml"
xmlfile2 = "test.xml"

tree1 = ET.parse(xmlfile1)
tree2 = ET.parse(xmlfile2)

root1 = tree1.getroot()
root2 = tree2.getroot()

root1.extend(root2)

tree1.write('output.xml')

But I am getting like:

<final_file>
   
        <career_profile>
            <output>
                <Template OriginalSentence="1" SentenceID="1" RecordID="0">
                    <Employer Type="String"><Value>HCL TECHNOLOGY LTD</Value></Employer>
                    <duration><Value>JAN 2018 to till date</Value></duration>
                </Template>
            </output>
        </career_profile>
    
</final_file>

I have tried with

random.xml:

<final_file>
    <final_output>

    </final_output>
</final_file>

But I am getting like this:

<final_file>
   <final_output>    </final_output>

       <career_profile>
            <output>
                <Template OriginalSentence="1" SentenceID="1" RecordID="0">
                    <Employer Type="String"><Value>HCL TECHNOLOGY LTD</Value></Employer>
                    <duration><Value>JAN 2018 to till date</Value></duration>
                </Template>
            </output>
        </career_profile>

</final_file>

Is there a way to do this without additional random.xml file.

The thing I am expecting is that to existing .xml file <finalfile*> tag is to be inserted at top and </*final_file> tag at bottom of the file.

CodePudding user response:

If you want to use xml package then the simple solution will be

from xml.etree import ElementTree

xml_input = "input.xml"
xml_output = "output.xml"

tree_input = ElementTree.parse(xml_input)
root = tree_input.getroot()

# add new tag
new_root = ElementTree.Element("final_file")
new_root.insert(0, root)

# format XML
ElementTree.indent(new_root)

print(ElementTree.dump(new_root))

with open(xml_output, "wb") as xml_file:
    xml_file.write(ElementTree.tostring(new_root))

in this case, the output will be

<final_file>
  <final_output>
    <career_profile>
      <output>
        <Template OriginalSentence="1" SentenceID="1" RecordID="0">
          <Employer Type="String">
            <Value>HCL TECHNOLOGY LTD</Value>
          </Employer>
          <duration>
            <Value>JAN 2018 to till date</Value>
          </duration>
        </Template>
      </output>
    </career_profile>
  </final_output>
</final_file>
  • Related