我正在尝试使用Python3.3将XML文件转换为YAML。 这是我的代码:
#! /etc/python3
test_filename_input = './reference-conversions/wikipedia-example.xml'
test_filename_output = 'wikipedia-example_xml_read-as-binary.yaml'
file_object = open( test_filename_input, 'rb')
data_in = file_object.read()
file_object.close()
from xml.dom.minidom import parseString
document_object = parseString( data_in)
import yaml
stream = open( test_filename_output, 'w')
yaml.dump( document_object, stream)
stream.close()
作为引用,我使用了来自here的XML文件:
<person>
<firstName>John</firstName>
<lastName>Smith</lastName>
<age>25</age>
<address>
<streetAddress>21 2nd Street</streetAddress>
<city>New York</city>
<state>NY</state>
<postalCode>10021</postalCode>
</address>
<phoneNumbers>
<phoneNumber type="home">212 555-1234</phoneNumber>
<phoneNumber type="fax">646 555-4567</phoneNumber>
</phoneNumbers>
<gender>
<type>male</type>
</gender>
</person>
结果应该是这样的:
---
firstName: John
lastName: Smith
age: 25
address:
streetAddress: 21 2nd Street
city: New York
state: NY
postalCode: 10021
phoneNumber:
-
type: home
number: 212 555-1234
-
type: fax
number: 646 555-4567
gender:
type: male
然而,结果是:
&id001 !!python/object/new:xml.dom.minidom.Document
state: !!python/tuple
- implementation: !!python/object:xml.dom.minidom.DOMImplementation {}
- _elem_info: {}
_id_cache: {}
_id_search_stack: null
childNodes: !!python/object/new:xml.dom.minicompat.NodeList
listitems:
- &id039 !!python/object/new:xml.dom.minidom.Element
state: !!python/tuple
- null
- _attrs: null
_attrsNS: null
childNodes: !!python/object/new:xml.dom.minicompat.NodeList
listitems:
- &id045 !!python/object/new:xml.dom.minidom.Text
state: !!python/tuple
- null
- _data: "\n "
nextSibling: &id002 !!python/object/new:xml.dom.minidom.Element
state: !!python/tuple
- null
- _attrs: null
_attrsNS: null
childNodes: !!python/object/new:xml.dom.minicompat.NodeList
listitems:
[...]
好的,如何让PyYAML从xml.dom.minidom中过滤出对象内容,或者使用xml.dom.minidom的任何替代方法?
谢谢!
我发现了一个XML to YAML converter,但我不得不在第92行做了一个小改动:
更改为
它删除输出中的任何
!!python/unicode
标记。我已经通过shell命令行测试了这个脚本,它工作得很好;我确信这只是一个简单的转换,可以让它在Python命令行中工作。编辑
我还添加了自己的打印方法,使输出更像您最初发布的内容:
然后在
convertXml2Yaml
函数中调用:这里有一种解决xml.dom问题的方法,并提供一种映射节点同时具有内容和属性或子节点的不明确情况的方法。对于上面的示例输入,它产生:
实现,yamlout.py:
相关问题 更多 >
编程相关推荐