Andrea Draghetti Andrea Draghetti - 5 months ago 25
Python Question

Python and XML Append for Feed RSS

I'm developing a simple Python script that generates an Feed RSS (XML).

But I can't append a new feed before the old, so as to have first rss always the latest news.

This is the code that I use to generate the initial XML file:

#!/usr/bin/env python
from lxml import etree as ET

root = ET.Element("rss")
root.set("version", "2.0")

channel = ET.SubElement(root, "channel")

title = ET.SubElement(channel, "title")
title.text = "W3Schools Home Page"

link = ET.SubElement(channel, "link")
link.text = "http://www.w3schools.com"

description = ET.SubElement(channel, "description")
description.text = "Free web building tutorials"

item = ET.SubElement(channel, "item")

title = ET.SubElement(item, "title")
title.text = "RSS Tutorial"

link = ET.SubElement(item, "link")
link.text = "http://www.w3schools.com/xml/xml_rss.asp"

description = ET.SubElement(item, "description")
description.text = "New RSS tutorial on W3Schools"

print ET.tostring(root, pretty_print=True, xml_declaration=True)
#write to file:
tree = ET.ElementTree(root)
tree.write('feed.xml', pretty_print=True, xml_declaration=True)


And this is the output:

<?xml version='1.0' encoding='ASCII'?>
<rss version="2.0">
<channel>
<title>W3Schools Home Page</title>
<link>http://www.w3schools.com</link>
<description>Free web building tutorials</description>
<item>
<title>RSS Tutorial</title>
<link>http://www.w3schools.com/xml/xml_rss.asp</link>
<description>New RSS tutorial on W3Schools</description>
</item>
</channel>
</rss>


But if I can try add a new XML value with this code:

#!/usr/bin/env python
from lxml import etree as ET
parser = ET.XMLParser(remove_blank_text=True)
tree = ET.parse("feed.xml", parser)
channel = tree.getroot()

item = ET.SubElement(channel, "item")

title = ET.SubElement(item, "title")
title.text = "Second Insert"
link = ET.SubElement(item, "link")
link.text = "http://second.test"
description = ET.SubElement(item, "description")
description.text = "description2"

channel[0].append(item)

print ET.tostring(channel, pretty_print=True, xml_declaration=True)
tree = ET.ElementTree(channel)
tree.write("feed.xml", pretty_print=True, xml_declaration=True)


The result is this:

<?xml version='1.0' encoding='ASCII'?>
<rss version="2.0">
<channel>
<title>W3Schools Home Page</title>
<link>http://www.w3schools.com</link>
<description>Free web building tutorials</description>
<item>
<title>RSS Tutorial</title>
<link>http://www.w3schools.com/xml/xml_rss.asp</link>
<description>New RSS tutorial on W3Schools</description>
</item>
**<item>
<title>Second Insert</title>
<link>http://second.test</link>
<description>description2</description>
</item>**
</channel>
</rss>


But I would like to get:

<?xml version='1.0' encoding='ASCII'?>
<rss version="2.0">
<channel>
<title>W3Schools Home Page</title>
<link>http://www.w3schools.com</link>
<description>Free web building tutorials</description>
**<item>
<title>Second Insert</title>
<link>http://second.test</link>
<description>description2</description>
</item>**
<item>
<title>RSS Tutorial</title>
<link>http://www.w3schools.com/xml/xml_rss.asp</link>
<description>New RSS tutorial on W3Schools</description>
</item>
</channel>
</rss>


I made several attempts, but do not understand where I'm wrong.

Who can help me?

Thank you
Andrea

Answer

You want to add it after the description node, which you can do with addnext:

channel.find(".//description").addnext(item)

print ET.tostring(channel, pretty_print=True, xml_declaration=True)

Or previous to the first item which you can do with addprevious:

channel.find(".//item").addprevious(item)

Both will give you:

<?xml version='1.0' encoding='ASCII'?>
<rss version="2.0">
  <channel>
    <title>W3Schools Home Page</title>
    <link>http://www.w3schools.com</link>
    <description>Free web building tutorials</description>
    <item>
      <title>Second Insert</title>
      <link>http://second.test</link>
      <description>description2</description>
    </item>
    <item>
      <title>RSS Tutorial</title>
      <link>http://www.w3schools.com/xml/xml_rss.asp</link>
      <description>New RSS tutorial on W3Schools</description>
    </item>
  </channel>
</rss>

channel[0] is the channel node so appending to that is just going to add the new nodes at the end.

Comments