0

Here is my code:

import xml.etree.ElementTree as ET

root = ET.Element("rss", version="2.0", xmlns:excerpt="http://wordpress.org/export/1.2/excerpt/", xmlns:content="http://purl.org/rss/1.0/modules/content/", xmlns:wfw="http://wellformedweb.org/CommentAPI/", xmlns:dc="http://purl.org/dc/elements/1.1/", xmlns:wp="http://wordpress.org/export/1.2/")

ET.dump(root)

This is xml file i am trying to create:

<rss version="2.0"
    xmlns:excerpt="http://wordpress.org/export/1.2/excerpt/"
    xmlns:content="http://purl.org/rss/1.0/modules/content/"
    xmlns:wfw="http://wellformedweb.org/CommentAPI/"
    xmlns:dc="http://purl.org/dc/elements/1.1/"
    xmlns:wp="http://wordpress.org/export/1.2/"
>

this is very basic part of actual xml file but i am unable to correctly create it with elementtree. Until version number it works correctly but when i add "xmlns:something" it does not work. I am very new to xml so i have no idea even google is not able to help me understand.

NOTE: Please do tell me if lxml is more easy or this ElementTree. Because I have previously used lxml for xpath and css elements.

2
  • 1
    I don't think that xmlns:excerpt is a valid python identifier. Use ET:element("rss", attrib={"xmlns:excerpt":"http://...", ...}) Commented Jun 21, 2016 at 12:00
  • thanks it helped. :) Commented Jun 21, 2016 at 12:18

1 Answer 1

1

Python identifiers are not allowed to have colons inside. ElementTree allows to pass an attribute dictionary by the key attrib:

ET.element("rss", attrib={"xmlns:excerpt":"http..."}

Sign up to request clarification or add additional context in comments.

3 Comments

I am having one more issue: whenever i set "ET.SubElement(wp_author, "wp:author_last_name").text = "<![CDATA[]]>"" it returns me this: encoded result: "<wp:author_last_name>&lt;![CDATA[]]&gt;</wp:author_last_name>"
How to decode these character before generating xml file.
If I undertand you correctly you want to insert CDATA into your xml. Here is one question (with answers), but there are others. If they don't work for you it is best if you create a new question here in SO.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.