Hello,
Thanks for your help. I do have assembled and run the script but there was an error while running it on
Platform section at line number 86 in MINiML.xml file. When i remove this line and run the script it prints correctly what we want in the output. The error in output prints like -
>>>
Traceback (most recent call last):
File "C:\Users\haojam\Desktop\GEO\GSE10006\test2.py ", line 45, in <module>
print "\n".join(nodeTree(contributor))
File "C:\Users\haojam\Desktop\GEO\GSE10006\test2.py ", line 32, in nodeTree
results.extend(nodeTree(node, pad+2))
File "C:\Users\haojam\Desktop\GEO\GSE10006\test2.py ", line 34, in nodeTree
results.append("%s%s" % ((pad+2)*" ", ", ".join(getTextFromElem(element))))
File "C:\Users\haojam\Desktop\GEO\GSE10006\test2.py ", line 15, in getTextFromElem
textList.append(str(n.nodeValue.strip()))
UnicodeEncodeError: 'ascii' codec can't encode character u'\xae' in position 379: ordinal not in range(128)
- #!/usr/bin/python
-
import xml.dom.minidom
-
-
# Load the Contibutor collection
-
MINiML = xml.dom.minidom.parse ( 'MINiML.xml' )
-
-
-
def getTextFromElem(parent):
-
'''Return a list of text found in the child nodes of a
-
parent node, discarding whitespace.'''
-
textList = []
-
for n in parent.childNodes:
-
# TEXT_NODE - 3
-
if n.nodeType == 3 and n.nodeValue.strip():
-
textList.append(str(n.nodeValue.strip()))
-
return textList
-
-
def getElemChildren(parent):
-
# Return a list of element nodes below parent
-
elements = []
-
for obj in parent.childNodes:
-
if obj.nodeType == obj.ELEMENT_NODE:
-
elements.append(obj)
-
return elements
-
-
def nodeTree(element, pad=0):
-
# Return list of strings representing the node tree below element
-
results = ["%s%s" % (pad*" ", str(element.nodeName))]
-
nextElems = getElemChildren(element)
-
if nextElems:
-
for node in nextElems:
-
results.extend(nodeTree(node, pad+2))
-
else:
-
results.append("%s%s" % ((pad+2)*" ", ", ".join(getTextFromElem(element))))
-
return results
-
-
contributors = MINiML.documentElement.getElementsByTagName( 'Contributor' )
-
for contributor in contributors:
-
print "\n".join(nodeTree(contributor))
-
contributors = MINiML.documentElement.getElementsByTagName( 'Database' )
-
for contributor in contributors:
-
print "\n".join(nodeTree(contributor))
-
contributors = MINiML.documentElement.getElementsByTagName( 'Platform' )
-
for contributor in contributors:
-
print "\n".join(nodeTree(contributor))
-
contributors = MINiML.documentElement.getElementsByTagName( 'Sample' )
-
for contributor in contributors:
-
print "\n".join(nodeTree(contributor))
-
contributors = MINiML.documentElement.getElementsByTagName( 'Series' )
-
for contributor in contributors:
-
print "\n".join(nodeTree(contributor))
-
Regards,
Haobijam