18 import xml.etree.ElementTree
as ET
27 Parses a XML file describing a grasp. 38 parses a given tree, returns a Grasp 41 xml_filename -- the filename where the grasp is defined 42 if no filename is provided, then the default 47 self.
xml_tree = ET.parse(xml_filename)
48 except Exception, inst:
49 print "Unexpected error opening %s: %s" % (xml_filename, inst)
54 tree_root = self.xml_tree.getroot()
55 tree_grasp = tree_root.findall(
"grasp")
57 for grasp
in tree_grasp:
59 grasp_tmp.grasp_name = grasp.attrib.get(
"name")
61 print "Grasp " + grasp_tmp.grasp_name
63 joints = grasp.findall(
"joint")
65 joint_name = j.attrib.get(
"name")
66 joint_position = float(j.text)
68 print " " + joint_name +
": " + str(joint_position)
70 grasp_tmp.joints_and_positions.update(
71 {joint_name: joint_position})
73 self.grasps.update({grasp_tmp.grasp_name: grasp_tmp})
76 if xml_filename ==
"":
79 toWrite = grasp.convert_to_xml()
80 objFileRead = open(xml_filename,
'r') 81 previous = objFileRead.readlines() 83 objFileWrite = open(xml_filename, 'w')
84 for index
in range(0, len(previous) - 1):
85 objFileWrite.write(previous[index])
86 objFileWrite.write(toWrite)
87 objFileWrite.write(
'</root>')
108 if __name__ ==
"__main__":
def write_grasp_to_file(self, grasp, xml_filename="")
def parse_tree(self, xml_filename="grasps.xml")