Python XML 序列化器

0 投票
1 回答
2367 浏览
提问于 2025-04-15 14:42

有没有人能推荐一个以元素或属性为中心的XML序列化工具,而且它不使用键值对的方式?

举个例子,GAE的db.model有一个to_xml()函数,但它输出的格式是这样的:

  <property name="firstname" type="string">John</property>
  <property name="lastname" type="string">Doe</property>
  <property name="city" type="string">Dallas</property>
  <property name="dateTimeCreated" type="gd:when">2009-09-30 19:45:45.975270</property>

我记得,这种格式在XSLT工具中比简单的元素或属性要难处理得多,像这样:

想要的输出格式

   <firstname>John</firstname>
   <lastname>Doe</lastname>
   <city>Dallas</city>
   <dateTimeCreated type="gd:when">2009-09-30 19:45:45.975270</dateTimeCreated>

我刚试了GNOSIS库,第一次尝试成功了,但也生成了类似于键值对的格式:

  <attr name="__coredata__" type="dict" id="4760164835402068688" >
    <entry>
      <key type="string">firstname</key>
      <val type="string">John</val>
    </entry>
    <entry>
      <key type="string">lastname</key>
      <val type="string">Doe</val>
    </entry> 
    etc... 

谢谢,

Neal Walters

1 个回答

2

pyxslt.serialize 看起来最接近你的需求,但并不是完全符合(比如,它不记录数据类型,只是把所有东西都变成字符串)。不过,这个模块仍然可以作为一个不错的基础,你可以在此基础上进行定制(如果它没有提供你需要的所有功能,可以考虑复制、粘贴和编辑来实现更干净的定制)。

撰写回答