Added xml creation functions
These functions are designed to help create an xml document.
This commit is contained in:
parent
7012c9a9af
commit
8b2a0b9433
|
@ -9,7 +9,6 @@ def attributes(node):
|
|||
attrs[item.name] = item.nodeValue
|
||||
return attrs
|
||||
|
||||
|
||||
def children(doc):
|
||||
list = []
|
||||
for node in doc.childNodes:
|
||||
|
@ -19,25 +18,38 @@ def children(doc):
|
|||
list += [node]
|
||||
return list
|
||||
|
||||
|
||||
def child(doc):
|
||||
list = children(doc)
|
||||
if len(list) > 0:
|
||||
return list[0]
|
||||
return None
|
||||
|
||||
def add_child(doc, node, name):
|
||||
child = doc.createElement(name)
|
||||
node.appendChild(child)
|
||||
return child
|
||||
|
||||
def add_text(doc, node, value):
|
||||
if value.__class__ == int or value.__class__ == long:
|
||||
value = str(value)
|
||||
elif value.__class__ == unicode:
|
||||
value = value.encode("utf-8")
|
||||
try:
|
||||
child = doc.createTextNode( value )
|
||||
node.appendChild(child)
|
||||
return value
|
||||
except:
|
||||
print value, value.__class__
|
||||
|
||||
def get_elements(doc, tag):
|
||||
return doc.getElementsByTagName(tag)
|
||||
|
||||
|
||||
def find_attribute(doc, tag, attr, value):
|
||||
for node in get_elements(doc, tag):
|
||||
if attributes(node)[attr] == value:
|
||||
return node
|
||||
return None
|
||||
|
||||
|
||||
def find_preferred_attribute(doc, tag, attr, values):
|
||||
for value in values:
|
||||
attr = find_attribute(doc, tag, attr, value)
|
||||
|
@ -45,6 +57,8 @@ def find_preferred_attribute(doc, tag, attr, values):
|
|||
return attr
|
||||
None
|
||||
|
||||
def new():
|
||||
return xml.Document()
|
||||
|
||||
def parse(xml_file):
|
||||
return xml.parse(xml_file)
|
||||
|
|
Loading…
Reference in New Issue