说我有这个XML:
<domain type='qemu' xmlns:qemu='http://libirt.org/schemas/domain/qemu/1.0'>
<name>QEmu-fedora-i686</name>
<memory>219200</memory>
<os>
<type arch='i686' machine='pc'>hvm</type>
</os>
<devices>
<emulator>/usr/bin/qemu-system-x86_64</emulator>
</devices>
<qemu:commandline>
<qemu:arg value='-newarg'/>
<qemu:env name='QEMU_ENV' value='VAL'/>
</qemu:commandline>
</domain>
此XML通过RelaxNG架构进行验证,该架构位于此处:
http://libvirt.org/git/?p=libvirt.git;a=tree;f=docs/schemas;hb=HEAD
现在我想从这个模式中生成类(持久性源文件),使我能够以面向对象的方式使用这个模型。所以我
最后,我希望能够做到这样的事情:
d = Domain()
d.name = 'QEmu-fedora-i686'
d.memory = 219200
d.os = Os('hvm')
d.os.type.arch = 'i686'
d.os.machine = 'pc'
...
我正在考虑自己编写类似的东西(通用的RelaxNG对象模型生成器),但我想知道是否有人可以帮助我如何启动,如果有一些python库可以帮助我这样做( LXML?)
方法1:将RelaxNG转换为XSD,然后使用generateDS生成对象模型
正如蒂托在他的回答中所说,我从here下载了最新的trang。然后我像这样执行了trang:java -jar trang.jar domain.rng domain.xsd
。这已经给了我一些警告:
/tmp/libvirt/schemas/domaincommon.rng:531:17: warning: cannot represent an optional group of attributes; approximating
/tmp/libvirt/schemas/domaincommon.rng:687:15: warning: choice between attributes and children cannot be represented; approximating
/tmp/libvirt/schemas/domaincommon.rng:955:15: warning: choice between attributes and children cannot be represented; approximating
/tmp/libvirt/schemas/domaincommon.rng:1041:15: warning: choice between attributes and children cannot be represented; approximating
/tmp/libvirt/schemas/domaincommon.rng:1260:15: warning: choice between attributes and children cannot be represented; approximating
/tmp/libvirt/schemas/domaincommon.rng:1817:17: warning: choice between attributes and children cannot be represented; approximating
/tmp/libvirt/schemas/domaincommon.rng:1808:15: warning: choice between attributes and children cannot be represented; approximating
/tmp/libvirt/schemas/domaincommon.rng:1924:15: warning: choice between attributes and children cannot be represented; approximating
/tmp/libvirt/schemas/domaincommon.rng:2240:15: warning: choice between attributes and children cannot be represented; approximating
不幸的是,尝试从生成的XSD中生成对象模型失败了:
$ generateDS.py domain.xsd
Traceback (most recent call last):
File "/usr/local/bin/generateDS.py", line 5, in <module>
pkg_resources.run_script('generateDS==2.7b', 'generateDS.py')
File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 467, in run_script
self.require(requires)[0].run_script(script_name, ns)
File "/usr/lib/python2.7/dist-packages/pkg_resources.py", line 1200, in run_script
execfile(script_filename, namespace, namespace)
File "/usr/local/lib/python2.7/dist-packages/generateDS-2.7b-py2.7.egg/EGG-INFO/scripts/generateDS.py", line 4709, in <module>
main()
File "/usr/local/lib/python2.7/dist-packages/generateDS-2.7b-py2.7.egg/EGG-INFO/scripts/generateDS.py", line 4703, in main
processIncludes, superModule=superModule)
File "/usr/local/lib/python2.7/dist-packages/generateDS-2.7b-py2.7.egg/EGG-INFO/scripts/generateDS.py", line 4433, in parseAndGenerate
inpath=xschemaFileName)
File "/usr/local/lib/python2.7/dist-packages/generateDS-2.7b-py2.7.egg/EGG-INFO/scripts/process_includes.py", line 49, in process_include_files
prep_schema_doc(infile, outfile, inpath, options)
File "/usr/local/lib/python2.7/dist-packages/generateDS-2.7b-py2.7.egg/EGG-INFO/scripts/process_includes.py", line 197, in prep_schema_doc
collect_inserts(root1, params, inserts, options)
File "/usr/local/lib/python2.7/dist-packages/generateDS-2.7b-py2.7.egg/EGG-INFO/scripts/process_includes.py", line 158, in collect_inserts
collect_inserts_aux(child, params, inserts, options)
File "/usr/local/lib/python2.7/dist-packages/generateDS-2.7b-py2.7.egg/EGG-INFO/scripts/process_includes.py", line 175, in collect_inserts_aux
collect_inserts(root, params, inserts, options)
File "/usr/local/lib/python2.7/dist-packages/generateDS-2.7b-py2.7.egg/EGG-INFO/scripts/process_includes.py", line 158, in collect_inserts
collect_inserts_aux(child, params, inserts, options)
File "/usr/local/lib/python2.7/dist-packages/generateDS-2.7b-py2.7.egg/EGG-INFO/scripts/process_includes.py", line 165, in collect_inserts_aux
root = etree.fromstring(string_content, base_url=params.base_url)
File "lxml.etree.pyx", line 2743, in lxml.etree.fromstring (src/lxml/lxml.etree.c:52665)
File "parser.pxi", line 1573, in lxml.etree._parseMemoryDocument (src/lxml/lxml.etree.c:79932)
File "parser.pxi", line 1452, in lxml.etree._parseDoc (src/lxml/lxml.etree.c:78774)
File "parser.pxi", line 960, in lxml.etree._BaseParser._parseDoc (src/lxml/lxml.etree.c:75389)
File "parser.pxi", line 564, in lxml.etree._ParserContext._handleParseResultDoc (src/lxml/lxml.etree.c:71739)
File "parser.pxi", line 645, in lxml.etree._handleParseResult (src/lxml/lxml.etree.c:72614)
File "parser.pxi", line 585, in lxml.etree._raiseParseError (src/lxml/lxml.etree.c:71955)
lxml.etree.XMLSyntaxError: attributes construct error, line 106, column 50
这些是由trang生成的XSD(并产生该错误):
http://mackaz.de/so/basictypes.xsd
http://mackaz.de/so/domain.xsd
http://mackaz.de/so/domaincommon.xsd
http://mackaz.de/so/networkcommon.xsd
http://mackaz.de/so/qemu.xsd
http://mackaz.de/so/storageencryption.xsd
通过一些调试,我发现了generateDS错误的来源。在文件basictypes.xsd中,似乎有一些错误的表达式(每个元素中有三个双引号):
<xs:simpleType name="filePath">
<xs:restriction base="xs:string">
<xs:pattern value="[a-zA-Z0-9_\.\+\-\\&"'<>/%]+"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="absFilePath">
<xs:restriction base="xs:string">
<xs:pattern value="/[a-zA-Z0-9_\.\+\-\\&"'<>/%]+"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="absDirPath">
<xs:restriction base="xs:string">
<xs:pattern value="/[a-zA-Z0-9_\.\+\-\\&"'<>/%]*"/>
</xs:restriction>
</xs:simpleType>
我用不同的值替换了这些表达式(现在它们不反映架构,但让generateDS开心):
<xs:simpleType name="filePath">
<xs:restriction base="xs:string">
<xs:pattern value="[a-zA-Z0-9\.\-]+"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="absFilePath">
<xs:restriction base="xs:string">
<xs:pattern value="[a-zA-Z0-9\.\-]+"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="absDirPath">
<xs:restriction base="xs:string">
<xs:pattern value="[a-zA-Z0-9\.\-]+"/>
</xs:restriction>
</xs:simpleType>
Et瞧 - 它有效,generateDS不再抱怨并生成这个输出文件:
现在我必须调查该文件并查看它是否可以帮助我(正如预期的那样,它非常大:28157 LOC ...)。
答案 0 :(得分:1)
如果您能够将rng
转换为xsd
(请查看conversions links on relaxng website),则可以使用generateDS项目。
parse()
,它会为您提供一个root python对象,其行为与您要求的相同。export()
函数答案 1 :(得分:0)
您可以使用具有RelaxNG验证和DOM对象接口的LXML,因此您可以像显示一样访问XML。但是,它不会为你生成类。