I am trying to open xml, remove whole tags and their contents and move other tags inside xml.
Here is my original xml import:
<?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> . Then I need to move the <provider> tag to the <video> and place it under the <original_spoken_locale> , and also move the <release_date> tag below the <video> .
Here is the xml export given:
<?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>
Now I have successfully installed lxml, so ideally looking for a solution.
Sincerely.
I managed to remove unnecessary tags and their contents, but still I need to be able to re-order / move other tags around, preferably without replacement. I also have a problem removing this xml "
Here is what I have now:
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')
Therefore, I still need to remove the <!--Carpet ID: fd54678--> . I want to remove them with wildcards, since there are many <!--.*--> , since the contents in the middle will change. and I also need to know how to move tag blocks around.