Discussion:
[libvirt-users] Python XML parsing of domain descriptions?
Stanley, Jon [Tech]
2010-10-01 16:19:15 UTC
Permalink
Does anyone know of a way to parse the libvirt XML into usable python data structures? Ideally, it would be something like foo['domain']['devices']['bridge'][0]['target'] or something of the sort. What I'm looking to accomplish is to figure out which tap interface goes with a specific domain.

I'm not keen on parsing XML to get that mapping, is there a better way to do it without parsing XML?
Cole Robinson
2010-10-01 18:20:48 UTC
Permalink
Post by Stanley, Jon [Tech]
Does anyone know of a way to parse the libvirt XML into usable python data structures? Ideally, it would be something like foo['domain']['devices']['bridge'][0]['target'] or something of the sort. What I'm looking to accomplish is to figure out which tap interface goes with a specific domain.
I'm not keen on parsing XML to get that mapping, is there a better way to do it without parsing XML?
I don't know of a solution off hand, but googling 'python xml to
dictionary' brings up a few people asking the same question, and some
possible solutions.

That said, just parsing the xml by hand isn't too bad, it only takes a
few minutes to learn how to contruct xpaths for lookup. Here's how to
get the target dev for the first interface device using libxml2 (and as
I understand it, the native python xml libraries are even simpler):


import libxml2

xmlstr = file("myxml.xml").read()
doc = libxml2.parseDoc(xmlstr)
ctx = doc.xpathNewContext()

nodelist = ctx.xpathEval("/domain/devices/interface[1]/target/@dev")
val = nodelist and nodelist[0].content or None
print val

- Cole
James M Leddy
2010-10-05 18:50:16 UTC
Permalink
rp.gs.com>


Jon,

Would you try with this patch?

diff -c libvirt-0.6.3/docs/schemas/domain.rng libvirt-0.6.3.updateschema/docs/schemas/domain.rng
*** libvirt-0.6.3/docs/schemas/domain.rng 2010-07-23 12:44:45.000000000 -0400
--- libvirt-0.6.3.updateschema/docs/schemas/domain.rng 2010-10-05 14:17:16.700561725 -0400
***************
*** 150,155 ****
--- 150,156 ----
</attribute>
<attribute name="machine">
<choice>
+ <value>rhel5.4.0</value>
<value>xenfv</value>
<value>pc</value>
<value>isapc</value>
--
James M. Leddy
Technical Account Manager
Red Hat Inc.
Daniel Veillard
2010-10-06 09:13:01 UTC
Permalink
Post by James M Leddy
rp.gs.com>
Jon,
Would you try with this patch?
diff -c libvirt-0.6.3/docs/schemas/domain.rng libvirt-0.6.3.updateschema/docs/schemas/domain.rng
*** libvirt-0.6.3/docs/schemas/domain.rng 2010-07-23 12:44:45.000000000 -0400
--- libvirt-0.6.3.updateschema/docs/schemas/domain.rng 2010-10-05 14:17:16.700561725 -0400
***************
*** 150,155 ****
--- 150,156 ----
</attribute>
<attribute name="machine">
<choice>
+ <value>rhel5.4.0</value>
<value>xenfv</value>
<value>pc</value>
<value>isapc</value>
Actually the upcoming RHEL-5.6 libvirt-client package will have something
which is based on current upstream i.e.:

<optional>
<attribute name="machine">
<data type="string">
<param name="pattern">[a-zA-Z0-9_\.\-]+</param>
</data>
</attribute>
</optional>

i.e. allowing any reasonable machine type string for the KVM hypervisor.

Daniel
--
Daniel Veillard | libxml Gnome XML XSLT toolkit http://xmlsoft.org/
***@veillard.com | Rpmfind RPM search engine http://rpmfind.net/
http://veillard.com/ | virtualization library http://libvirt.org/
Loading...