I am trying to update an iTunes podcast XML and need to put information above the first item tag. I have tried using minidom and lxml.etree, but cannot seem to drill down far enough to begin inserting where I need to. Here is the XML and the code I have tried most recently, which errors out because lxml turns the child nodes into a list.
<?xml version='1.0' encoding='UTF-8'?>
<rss xmlns:itunes="http://ift.tt/IR9se2" version="2.0">
<channel>
<title>Title</title>
<link>weblink</link>
<language>en-us</language>
<copyright>Copyright</copyright>
<itunes:subtitle>Subtitle</itunes:subtitle>
<itunes:author>Author</itunes:author>
<itunes:summary>Summary</itunes:summary>
<description>Description</description>
<itunes:owner>
<itunes:name>Owner Name</itunes:name>
<itunes:email>Owner email</itunes:email>
</itunes:owner>
<itunes:image image.jpg"/>
<itunes:category text="MISC">
<itunes:category text="MISC"/>
</itunes:category>
<itunes:block>no</itunes:block>
<itunes:explicit>no</itunes:explicit>
<item>
<title>First Entry</title>
<itunes:author>Author</itunes:author>
<itunes:subtitle>Just Another Entry</itunes:subtitle>
<itunes:summary>Podcast Summary.</itunes:summary>
<enclosure url="url_to_mp3" length="48412967" type="audio/mpeg"/>
<guid>url_to_mp3</guid>
<pubDate>Sun, 12 Oct 2014 12:00:00 -0500</pubDate>
<itunes:duration>00:50:26</itunes:duration>
<itunes:block>no</itunes:block>
<itunes:explicit>no</itunes:explicit>
</item>
My current code:
from lxml import etree
tree = etree.parse(file)
root = tree.getroot()
child = root.getchildren()
child.insert(13, etree.Element('item'))
child[13].insert(0, etree.SubElement(child[13], 'title'))
Thank you for any help you can provide.
No comments:
Post a Comment