简体   繁体   English

在python中模板化xml文件的快捷方法

[英]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. 现在我已经在我的python脚本中硬编码了整个xml文件,只是做了out.write(),但是现在它变得越来越难以管理,因为我有多种类型的xml文件。

What is the easiest and quickest way to setup templating so that I can just give the variable names amd filename? 设置模板的最简单,最快捷的方法是什么,以便我可以给变量名称amd filename?

A lightweight option is xml.dom.minidom 轻量级选项是xml.dom.minidom

xml.dom.minidom is a light-weight implementation of the Document Object Model interface. xml.dom.minidom是文档对象模型接口的轻量级实现。 It is intended to be simpler than the full DOM and also significantly smaller. 它旨在比完整的DOM更简单,也更小。

You can create DOM object using the xml.dom API, for example DOM Element objects , and generate the XML using Node.writexml . 您可以使用xml.dom API创建DOM对象,例如DOM Element对象 ,并使用Node.writexml生成XML。 Note that this requires building DOM hierarchies, which may not be what you are after. 请注意,这需要构建DOM层次结构,这可能不是您所追求的。

more pythonic option is ElementTree . 更多pythonic选项是ElementTree

The Element type is a flexible container object, designed to store hierarchical data structures in memory. Element类型是一个灵活的容器对象,旨在将分层数据结构存储在内存中。 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() ElementTree对象在Python中更容易创建和处理,并且可以使用ElementTree.dump()ElementTree.tostring()序列化为XML

Two choices. 两个选择。

  1. A template tool, for example Jinja2 . 模板工具,例如Jinja2

  2. Build the DOM object. 构建DOM对象。 Not as bad as it sounds. 没有听起来那么糟糕。 ElementTree has a pleasant factory for building XML tags and creating the necessary structure. ElementTree有一个令人愉快的工厂,用于构建XML标签和创建必要的结构。

Short answer is: You should be focusing, and dealing with, the data (ie, python object) and not the raw XML 简短的回答是:您应该关注和处理数据(即python对象)而不是原始XML

Basic story: XML is supposed to be a representation of some data, or data set. 基本故事: XML应该是某些数据或数据集的表示。 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. Python选择: BeautifulSoup,lxml和其他python库(ElementTree等),使得处理XML更加容易。 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. 与我试图直接使用原始格式的XML相比,它们让我更容易读入或写出XML数据。

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. 在这两个(输入,输出)活动的中间,我的python程序正在处理一个很好的python对象或我可以走的某种解析树。 You can read data in, create an object from that string, manipulate it and write out XML. 您可以读取数据,从该字符串创建对象,操作它并写出XML。

Other choice, Templates: OK -- maybe you like XML and just want to "template" it so you can populate it with the data. 其他选择,模板:好的 - 也许您喜欢XML并且只想“模板化”它以便您可以使用数据填充它。

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. 而且,这类似于您当前使用的XML字符串 - 因此可能更为熟悉。

Use Cheetah, Jinja, or other template libraries to help. 使用Cheetah,Jinja或其他模板库来提供帮助。 Make a template for the XML file, using that template language. 使用该模板语言为XML文件创建模板。

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. 您可以使用模板将此书对象列表传递给模板引擎,然后告诉它写出您的XML输出。

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. 模板引擎将遍历“object_list”并输出包含所有书籍的长XML文件。 That would be much better than storing raw XML strings, as you currently are. 这比存储原始XML字符串好得多,就像你现在一样。

This makes the update & modification of the display of XML separate from the data, data storage, and data manipulation -- making your life easier. 这使得XML显示的更新和修改与数据,数据存储和数据操作分开 - 使您的生活更轻松。

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. 如果你想要一些非常简单和轻巧的东西,我的古代YAPTU和Palmer的yaptoo变种应该是可用的 - 但是现在有许多其他通用和强大的模板引擎可供选择。 A pretty complete list is here . 这里有一个非常完整的清单。

You asked for the easiest and quickest, so see this post: http://blog.simonwillison.net/post/58096201893/simpletemplates 你要求最简单,最快,所以请看这篇文章: http//blog.simonwillison.net/post/58096201893/simpletemplates

If you want something smarter, take a look here . 如果你想要更聪明的东西,请看看这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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