def from_xml(self, content): tree = etree.fromstring(content) NSMAP = ['codeList', 'codeListValue', 'schemaLocation'] etree.strip_attributes(tree, *NSMAP) root = tree etree.cleanup_namespaces(root) namespaces = { 'gvq': '', 'gco': '', 'gmd19157': '', 'updated19115': '', 'gmd': '', 'gml': '', 'xsi': '', 'xmlns': '', } #Some fields are xml text fields. The fields must be unique inside the model. # Store those elements temporarily. listOfTextFields = getXMLTextFields() fieldDictionary = dict() namespaceDict, originalNamespace = getNamespaceInfo() for field in listOfTextFields: foundElements = root.findall(".//" + namespaceDict[field] + field, namespaces=originalNamespace) valueList = list() for element in foundElements: valueList.append(''.join( [etree.tostring(child) for child in element])) fieldDictionary[field] = valueList xmlcontent = etree.tostring(root) jsondata = xmltodict.parse(xmlcontent, namespaces=namespaces) #Replace all textFields with the xml text. for field in listOfTextFields: i = 0 replaceDictValueWithStringElement(jsondata, field, i, fieldDictionary) if 'GVQ_FeedbackCollection' in jsondata: item = jsondata['GVQ_FeedbackCollection']['item'] elif 'item' in jsondata: item = jsondata['item'] else: item = jsondata self.from_json(json.dumps(item)) return item
def from_xml(self, content): tree = etree.fromstring(content) NSMAP = ['codeList', 'codeListValue', 'schemaLocation'] etree.strip_attributes(tree, *NSMAP) root = tree etree.cleanup_namespaces(root) namespaces = { 'gvq' :'', 'gco' :'', 'gmd19157' :'', 'updated19115':'', 'gmd' :'', 'gml' :'', 'xsi' :'', 'xmlns' :'', } #Some fields are xml text fields. The fields must be unique inside the model. # Store those elements temporarily. listOfTextFields = getXMLTextFields() fieldDictionary = dict() namespaceDict, originalNamespace = getNamespaceInfo() for field in listOfTextFields: foundElements = root.findall(".//"+namespaceDict[field]+field, namespaces=originalNamespace) valueList = list() for element in foundElements: valueList.append(''.join([etree.tostring(child) for child in element])) fieldDictionary[field] = valueList xmlcontent = etree.tostring(root) jsondata = xmltodict.parse(xmlcontent, namespaces=namespaces) #Replace all textFields with the xml text. for field in listOfTextFields: i = 0 replaceDictValueWithStringElement(jsondata, field, i, fieldDictionary) if 'GVQ_FeedbackCollection' in jsondata: item = jsondata['GVQ_FeedbackCollection']['item'] elif 'item' in jsondata: item = jsondata['item'] else: item = jsondata self.from_json(json.dumps(item)) return item
def to_xml(self, data, options = None): options = options or {} data = self.to_json(data) data = json.loads(data) # Load namespaces and attributevalues specific for this serializer. self.attributeDictionary = getAttributeDictionary() self.attributeValueDictionary = getAttributeValueDictionary() namespaceDict, NSMAP = getNamespaceInfo() data = self.to_customEtree(data, namespaceDict, options, nsmap=NSMAP) etree.strip_tags(data, "itemCollection") ##Create string data. data = etree.tostring(data) return data
def to_xml(self, data, options=None): options = options or {} data = self.to_json(data) data = json.loads(data) # Load namespaces and attributevalues specific for this serializer. self.attributeDictionary = getAttributeDictionary() self.attributeValueDictionary = getAttributeValueDictionary() namespaceDict, NSMAP = getNamespaceInfo() data = self.to_customEtree(data, namespaceDict, options, nsmap=NSMAP) etree.strip_tags(data, "itemCollection") ##Create string data. data = etree.tostring(data) return data