[Kimchi-devel] [PATCH 1/3] Use lxml.etree on xmlutils/utils.py instead of xml.etree and libxml2

Aline Manera alinefm at linux.vnet.ibm.com
Wed Oct 29 16:09:12 UTC 2014


xml.etree does not work well when the XML has non-registered namespaces.
And as libvirt allows anyone to insert custom XML on <metadata> element
it can be an easily point of failure while manipulating guest XML.
To avoid problems with that use lxml.etree

Signed-off-by: Aline Manera <alinefm at linux.vnet.ibm.com>
---
 src/kimchi/xmlutils/utils.py | 18 ++++++------------
 1 file changed, 6 insertions(+), 12 deletions(-)

diff --git a/src/kimchi/xmlutils/utils.py b/src/kimchi/xmlutils/utils.py
index 2af1c2c..bfbe0c1 100644
--- a/src/kimchi/xmlutils/utils.py
+++ b/src/kimchi/xmlutils/utils.py
@@ -17,27 +17,21 @@
 # License along with this library; if not, write to the Free Software
 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
 
-import libxml2
+import lxml.etree as ET
 from lxml import objectify
 
 
-from xml.etree import ElementTree
-
-
 def xpath_get_text(xml, expr):
-    doc = libxml2.parseDoc(xml)
-    res = doc.xpathEval(expr)
-    ret = [None if x.children is None else x.children.content for x in res]
-
-    doc.freeDoc()
-    return ret
+    doc = ET.fromstring(xml)
+    res = [x.text if not isinstance(x, str) else x for x in doc.xpath(expr)]
+    return res
 
 
 def xml_item_update(xml, xpath, value):
-    root = ElementTree.fromstring(xml)
+    root = ET.fromstring(xml)
     item = root.find(xpath)
     item.text = value
-    return ElementTree.tostring(root, encoding="utf-8")
+    return ET.tostring(root, encoding="utf-8")
 
 
 def dictize(xmlstr):
-- 
1.9.3




More information about the Kimchi-devel mailing list