I am trying to open an xml, delete whole tags and their contents, and move other tags around within the xml.
Here is my original import xml:
<?xml version="1.0" encoding="UTF-8"?>
<package>
<language>en-GB</language>
<video>
<original_spoken_locale>en-US</original_spoken_locale>
<copyright_cline>2012 copyright</copyright_cline>
<release_date>2012-04-23</release_date>
<title>Amazing Film</title>
</video>
<provider>testprovider</provider>
</package>
I need to remove the <copyright_cline> tag and the <title> tag. Then I need to move the <provider> tag up into the <video> tag and position it between below the <original_spoken_locale> tag and also move the <release_date> tag down below the <video> tag.
Here is the resulted export xml:
<?xml version="1.0" encoding="UTF-8"?>
<package>
<language>en-GB</language>
<video>
<original_spoken_locale>en-US</original_spoken_locale>
<provider>testprovider</provider>
<release_date>2012-04-23</release_date>
</video>
<release_date>2012-04-23</release_date>
</package>
I have now sucsessfully installed lxml, so looking for a solution for that ideally.
Kind regards.
I have been able to remove the unwanted tags and their contents, but still need to be able to re-order / move the other tags around, preferably without replacing. I'm also having trouble removing this line of xml code"
<!--Carpet ID: fd54678-->
Here is what I currently have:
from lxml import etree
xmlFileIn = '/xmls/metadata.xml'
xmlFileOut = '/xmls/output.xml'
tree = etree.parse(xmlFileIn)
root = tree.getroot()
etree.strip_elements(root, 'assets')
etree.strip_tags(root, 'assets')
etree.strip_elements(root, 'chapters')
etree.strip_tags(root, 'chapters')
etree.strip_elements(root, 'xid')
etree.strip_tags(root, 'xid')
# Write the new xml file
tree.write(xmlFileOut, pretty_print=True, xml_declaration=True, encoding="utf-8")
So I still need to remove the <!--Carpet ID: fd54678--> tag. I want to remove these via wildcards as there are many <!--.*-->, as the content in the middle will change. and I also need to know how to move blocks of tags around.