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
|
attrs[item.name] = item.nodeValue
|
||||||
return attrs
|
return attrs
|
||||||
|
|
||||||
|
|
||||||
def children(doc):
|
def children(doc):
|
||||||
list = []
|
list = []
|
||||||
for node in doc.childNodes:
|
for node in doc.childNodes:
|
||||||
|
@ -19,25 +18,38 @@ def children(doc):
|
||||||
list += [node]
|
list += [node]
|
||||||
return list
|
return list
|
||||||
|
|
||||||
|
|
||||||
def child(doc):
|
def child(doc):
|
||||||
list = children(doc)
|
list = children(doc)
|
||||||
if len(list) > 0:
|
if len(list) > 0:
|
||||||
return list[0]
|
return list[0]
|
||||||
return None
|
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):
|
def get_elements(doc, tag):
|
||||||
return doc.getElementsByTagName(tag)
|
return doc.getElementsByTagName(tag)
|
||||||
|
|
||||||
|
|
||||||
def find_attribute(doc, tag, attr, value):
|
def find_attribute(doc, tag, attr, value):
|
||||||
for node in get_elements(doc, tag):
|
for node in get_elements(doc, tag):
|
||||||
if attributes(node)[attr] == value:
|
if attributes(node)[attr] == value:
|
||||||
return node
|
return node
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
||||||
def find_preferred_attribute(doc, tag, attr, values):
|
def find_preferred_attribute(doc, tag, attr, values):
|
||||||
for value in values:
|
for value in values:
|
||||||
attr = find_attribute(doc, tag, attr, value)
|
attr = find_attribute(doc, tag, attr, value)
|
||||||
|
@ -45,6 +57,8 @@ def find_preferred_attribute(doc, tag, attr, values):
|
||||||
return attr
|
return attr
|
||||||
None
|
None
|
||||||
|
|
||||||
|
def new():
|
||||||
|
return xml.Document()
|
||||||
|
|
||||||
def parse(xml_file):
|
def parse(xml_file):
|
||||||
return xml.parse(xml_file)
|
return xml.parse(xml_file)
|
||||||
|
|
Loading…
Reference in New Issue