def _convert_xml_to_entity(xmlstr): ''' Convert xml response to entity. The format of entity: <entry xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom"> <title /> <updated>2008-09-18T23:46:19.3857256Z</updated> <author> <name /> </author> <id /> <content type="application/xml"> <m:properties> <d:Address>Mountain View</d:Address> <d:Age m:type="Edm.Int32">23</d:Age> <d:AmountDue m:type="Edm.Double">200.23</d:AmountDue> <d:BinaryData m:type="Edm.Binary" m:null="true" /> <d:CustomerCode m:type="Edm.Guid">c9da6455-213d-42c9-9a79-3e9149a57833</d:CustomerCode> <d:CustomerSince m:type="Edm.DateTime">2008-07-10T00:00:00</d:CustomerSince> <d:IsActive m:type="Edm.Boolean">true</d:IsActive> <d:NumOfOrders m:type="Edm.Int64">255</d:NumOfOrders> <d:PartitionKey>mypartitionkey</d:PartitionKey> <d:RowKey>myrowkey1</d:RowKey> <d:Timestamp m:type="Edm.DateTime">0001-01-01T00:00:00</d:Timestamp> </m:properties> </content> </entry> ''' xmldoc = minidom.parseString(xmlstr) xml_properties = None for entry in _get_child_nodes(xmldoc, 'entry'): for content in _get_child_nodes(entry, 'content'): # TODO: Namespace xml_properties = _get_child_nodesNS(content, METADATA_NS, 'properties') if not xml_properties: return None entity = Entity() # extract each property node and get the type from attribute and node value for xml_property in xml_properties[0].childNodes: name = _remove_prefix(xml_property.nodeName) if xml_property.firstChild: value = xml_property.firstChild.nodeValue else: value = '' isnull = xml_property.getAttributeNS(METADATA_NS, 'null') mtype = xml_property.getAttributeNS(METADATA_NS, 'type') # if not isnull and no type info, then it is a string and we just # need the str type to hold the property. if not isnull and not mtype: _set_entity_attr(entity, name, value) elif isnull == 'true': if mtype: property = EntityProperty(mtype, None) else: property = EntityProperty('Edm.String', None) else: # need an object to hold the property conv = _ENTITY_TO_PYTHON_CONVERSIONS.get(mtype) if conv is not None: property = conv(value) else: property = EntityProperty(mtype, value) _set_entity_attr(entity, name, property) # extract id, updated and name value from feed entry and set them of # rule. for name, value in _get_entry_properties(xmlstr, True).items(): if name in ['etag']: _set_entity_attr(entity, name, value) return entity
def _convert_xml_to_entity(xmlstr): ''' Convert xml response to entity. The format of entity: <entry xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom"> <title /> <updated>2008-09-18T23:46:19.3857256Z</updated> <author> <name /> </author> <id /> <content type="application/xml"> <m:properties> <d:Address>Mountain View</d:Address> <d:Age m:type="Edm.Int32">23</d:Age> <d:AmountDue m:type="Edm.Double">200.23</d:AmountDue> <d:BinaryData m:type="Edm.Binary" m:null="true" /> <d:CustomerCode m:type="Edm.Guid">c9da6455-213d-42c9-9a79-3e9149a57833</d:CustomerCode> <d:CustomerSince m:type="Edm.DateTime">2008-07-10T00:00:00</d:CustomerSince> <d:IsActive m:type="Edm.Boolean">true</d:IsActive> <d:NumOfOrders m:type="Edm.Int64">255</d:NumOfOrders> <d:PartitionKey>mypartitionkey</d:PartitionKey> <d:RowKey>myrowkey1</d:RowKey> <d:Timestamp m:type="Edm.DateTime">0001-01-01T00:00:00</d:Timestamp> </m:properties> </content> </entry> ''' xmldoc = minidom.parseString(xmlstr) xml_properties = None for entry in _get_child_nodes(xmldoc, 'entry'): for content in _get_child_nodes(entry, 'content'): xml_properties = _get_child_nodesNS(content, METADATA_NS, 'properties') # TODO: Namespace if not xml_properties: return None entity = Entity() #extract each property node and get the type from attribute and node value for xml_property in xml_properties[0].childNodes: if xml_property.firstChild: name = _remove_prefix(xml_property.nodeName) #exclude the Timestamp since it is auto added by azure when inserting #entity. We don't want this to mix with real properties if name in ['Timestamp']: continue value = xml_property.firstChild.nodeValue isnull = xml_property.getAttributeNS(METADATA_NS, 'null') mtype = xml_property.getAttributeNS(METADATA_NS, 'type') #if not isnull and no type info, then it is a string and we just need the str type to hold the property. if not isnull and not mtype: _set_entity_attr(entity, name, value) elif isnull == 'true': if mtype: property = EntityProperty(mtype, None) else: property = EntityProperty('Edm.String', None) else: #need an object to hold the property conv = _ENTITY_TO_PYTHON_CONVERSIONS.get(mtype) if conv is not None: property = conv(value) else: property = EntityProperty(mtype, value) _set_entity_attr(entity, name, property) #extract id, updated and name value from feed entry and set them of rule. for name, value in _get_entry_properties(xmlstr, True).iteritems(): if name in ['etag']: _set_entity_attr(entity, name, value) return entity