[Kimchi-devel] [PATCH] [Wok] Add function for inserting an XML element

Lucio Correia luciojhc at linux.vnet.ibm.com
Wed Feb 3 19:31:37 UTC 2016


- Plus a minor fix

Signed-off-by: Lucio Correia <luciojhc at linux.vnet.ibm.com>
---
 src/wok/xmlutils/utils.py | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/src/wok/xmlutils/utils.py b/src/wok/xmlutils/utils.py
index d02139c..cabb859 100644
--- a/src/wok/xmlutils/utils.py
+++ b/src/wok/xmlutils/utils.py
@@ -36,6 +36,15 @@ def xpath_get_text(xml, expr):
     return res
 
 
+def xml_item_insert(xml, xpath, item_xml):
+    root = ET.fromstring(xml)
+
+    element = root.find(xpath)
+    element.insert(0, ET.fromstring(item_xml))
+
+    return ET.tostring(root, encoding="utf-8")
+
+
 def xml_item_update(xml, xpath, value, attr=None):
     root = ET.fromstring(xml)
     item = root.find(xpath)
@@ -51,7 +60,7 @@ def xml_item_remove(xml, xpath):
 
     element = root.find(xpath)
     parent = root.find(xpath + "/..")
-    if parent:
+    if parent is not None:
         parent.remove(element)
 
     return ET.tostring(root, encoding="utf-8")
-- 
1.9.1




More information about the Kimchi-devel mailing list