Generate XML (RSS) with Python

Introduction

I had to output RSS ver2 with Python, but I made a note because it was moss several times at that time.

Python has a high affinity with json, but XML seems awkward. I have some libraries as below, but I can't realize what I want to do.

manner

After all, I settled down by converting from a DOM object to XML.

When generating from an XML template of a string

#!/usr/bin/env python
# coding: utf-8

from xml.dom.minidom import parseString

xml_template = "<rss version=\"2.0\">\
        <channel>\
            <title>title</title>\
            <link>link</link>\
            <description>desctiption</description>\
            <language>ja</language>\
        </channel></rss>"
dom = parseString(xml_template)

#Get channel node
channel = dom.getElementsByTagName("channel")[0]

#Generate item node
item = dom.createElement('item')
#Add to channel node
channel.appendChild(item)

#Subnode generation
subnode = dom.createElement('subnode')
subnode.appendChild(dom.createTextNode("Japanese is also OK"))
#Set attribute and value in subnote
subnode_attr = dom.createAttribute('key')
subnode_attr.value = 'value'
subnode.setAttributeNode(subnode_attr)
#Add subnode node to item node
item.appendChild(subnode)

#Convert dom to xml and format
print (dom.toprettyxml())

output

<?xml version="1.0"?>
<rss version="2.0">
  <channel>
    <title>title</title>
    <link>link</link>
    <description>desctiption</description>
    <language>ja</language>
    <item>
      <subnode key="value">Japanese is also OK</subnode>
    </item>
  </channel>
</rss>

When generating XML from 1

#!/usr/bin/env python
# coding: utf-8

import xml.dom.minidom

#Generating a DOM object
dom = xml.dom.minidom.Document()

#Creating and adding root nodes
root = dom.createElement('root')
dom.appendChild(root)

#Subnode generation
subnode = dom.createElement('subnode')
subnode.appendChild(dom.createTextNode("Japanese is also OK"))
#Set attribute and value in subnote
subnode_attr = dom.createAttribute('key')
subnode_attr.value = 'value'
subnode.setAttributeNode(subnode_attr)
#Add subnode node to item node
root.appendChild(subnode)

#Convert dom to xml and format
print (dom.toprettyxml())

output

<?xml version="1.0"?>
<root>
  <subnode key="value">Japanese is also OK</subnode>
</root>

Summary

json best

Reference

Recommended Posts

Generate XML (RSS) with Python
Scraping RSS with Python
Process feedly xml with Python.
[Python] Generate a password with Slackbot
Process Pubmed .xml data with python
FizzBuzz with Python3
Scraping with Python
Statistics with python
Process Pubmed .xml data with python [Part 2]
Scraping with Python
Python with Go
Generate Japanese test data with Python faker
Twilio with Python
Integrate with Python
Play with 2016-Python
AES256 with python
Tested with Python
python starts with ()
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
Excel with Python
Microcomputer with Python
Cast with python
Library comparison summary to generate PDF with Python
Generate two correlated pseudo-random numbers (with Python sample)
Try to automatically generate Python documents with Sphinx
Generate an insert statement from CSV with Python.
Compare xml parsing speeds with Python and Go
Generate n correlated pseudo-random numbers (with Python sample)
Serial communication with Python
Zip, unzip with python
Django 1.11 started with Python3.6
Primality test with Python
Python with eclipse + PyDev.
Socket communication with Python
Data analysis with python 2
Scraping with Python (preparation)
Learning Python with ChemTHEATER 03
Sequential search with Python
Run Python with VBA
Handling yaml with python
Solve AtCoder 167 with python
Serial communication with python
[Python] Use JSON with Python
Learning Python with ChemTHEATER 05-1
Learn Python with ChemTHEATER
Run prepDE.py with python3
1.1 Getting Started with Python
Collecting tweets with Python
Binarization with OpenCV / Python
3. 3. AI programming with Python
Kernel Method with Python
Non-blocking with Python + uWSGI
Scraping with Python + PhantomJS
Parse XML in Python
Posting tweets with python
Drive WebDriver with python
Use mecab with Python3
[Python] Redirect with CGIHTTPServer
Voice analysis with python