简体   繁体   中英

fast and easy way to template xml files in python

Right now I've hard coded the whole xml file in my python script and just doing out.write(), but now it's getting harder to manage because i have multiple types of xml file.

What is the easiest and quickest way to setup templating so that I can just give the variable names amd filename?

A lightweight option is xml.dom.minidom

xml.dom.minidom is a light-weight implementation of the Document Object Model interface. It is intended to be simpler than the full DOM and also significantly smaller.

You can create DOM object using the xml.dom API, for example DOM Element objects , and generate the XML using Node.writexml . Note that this requires building DOM hierarchies, which may not be what you are after.

more pythonic option is ElementTree .

The Element type is a flexible container object, designed to store hierarchical data structures in memory. The type can be described as a cross between a list and a dictionary.

ElementTree objects are easier to create and handle in Python , and can be serialized to XML with ElementTree.dump() or ElementTree.tostring()

Two choices.

  1. A template tool, for example Jinja2 .

  2. Build the DOM object. Not as bad as it sounds. ElementTree has a pleasant factory for building XML tags and creating the necessary structure.

Short answer is: You should be focusing, and dealing with, the data (ie, python object) and not the raw XML

Basic story: XML is supposed to be a representation of some data, or data set. You don't have a lot of detail in your question about the type of data, what it represents, etc, etc -- so I'll give you some basic answers.

Python choices: BeautifulSoup, lxml and other python libraries (ElementTree, etc.), make dealing with XML more easy. They let me read in, or write out, XML data much more easily than if I'd tried to work directly with the XML in raw form.

In the middle of those 2 (input,output) activities, my python program is dealing with a nice python object or some kind of parse tree I can walk. You can read data in, create an object from that string, manipulate it and write out XML.

Other choice, Templates: OK -- maybe you like XML and just want to "template" it so you can populate it with the data.

You might be more comfortable with this, if you aren't really manipulating the data -- but just representing it for output. And, this is similar to the XML strings you are currently using -- so may be more familiar.

Use Cheetah, Jinja, or other template libraries to help. Make a template for the XML file, using that template language.

For example, you just read a list of books from a file or database table. You would pass this list of book objects to the template engine, with a template, and then tell it to write out your XML output.

Example template for these book objects:

<?xml version="1.0"?>
<catalog>
   {% for object in object_list %}
   <book id="{{ object.bookID }}">
      <author>{{ object.author_name }}</author>
      <title>{{ object.title }}</title>
      <genre>{{ object.genre }}</genre>
      <price>{{ object.price }}</price>
      <publish_date>{{ object.pub_date }}</publish_date>
      <description>{{ object.description }}</description>
   </book>
   {% endfor %}
 </catalog>
 </xml>

The template engine would loop through the "object_list" and output a long XML file with all your books. That would be much better than storing raw XML strings, as you currently are.

This makes the update & modification of the display of XML separate from the data, data storage, and data manipulation -- making your life easier.

My ancient YAPTU and Palmer's yaptoo variant on it should be usable if you want something very simple and lightweight -- but there are many, many other general and powerful templating engines to chose among, these days. A pretty complete list is here .

You asked for the easiest and quickest, so see this post: http://blog.simonwillison.net/post/58096201893/simpletemplates

If you want something smarter, take a look here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM