def _read_and_deserialize_dataone_type(self, response): """Given a response body, try to create an instance of a DataONE type. The return value will be either an instance of a type or a DataONE exception. """ response_body = response.content try: return self._types.CreateFromDocument(response_body) except pyxb.SimpleFacetValueError as e: # example: <blockedMemberNode/> is not allowed since it is a zero length # string if not self.retry_SimpleFacetValueError: self._raise_service_failure_invalid_dataone_type( response, response_body, str(e)) except pyxb.IncompleteElementContentError as e: # example: <accessPolicy/> is not allowed since it requires 1..n # AccessRule entries if not self.retry_IncompleteElementContentError: self._raise_service_failure_invalid_dataone_type( response, response_body, str(e)) #Continue, retry without validation self.logger.warn( "Retrying deserialization without schema validation...") validation_setting = pyxb.RequireValidWhenParsing() try: pyxb.RequireValidWhenParsing(False) return self._types.CreateFromDocument(response_body) except pyxb.PyXBException as e: pyxb.RequireValidWhenParsing(validation_setting) self._raise_service_failure_invalid_dataone_type( response, response_body, str(e)) finally: # always make sure the validation flag is set to the original when exiting pyxb.RequireValidWhenParsing(validation_setting)
def _loadMore(self, start=0, trys=0, validation=True): """Retrieves the next page of results """ self.log.debug("Loading page starting from %d" % start) self._czero = start self._pageoffs = 0 try: pyxb.RequireValidWhenParsing(validation) self._object_list = self._client.listObjects( start=start, count=self._pagesize, fromDate=self._fromDate, nodeId=self._nodeId ) except http.client.BadStatusLine as e: self.log.warning("Server responded with Bad Status Line. Retrying in 5sec") self._client.connection.close() if trys > 3: raise e trys += 1 self._loadMore(start, trys) except d1_common.types.exceptions.ServiceFailure as e: self.log.error(e) if trys > 3: raise e trys += 1 self._loadMore(start, trys, validation=False)
def file_to_xml_dom_object(filename): with open(filename, 'r') as infile: pyxb.RequireValidWhenParsing(False) xml_data = infile.read() dom = resources.musicxml.CreateFromDocument(xml_data) return dom
def cgml2class(xml_path): xml = open(xml_path).read() pyxb.RequireValidWhenParsing( False) #do not validate xml file when parsing to binding instance try: cgml_model = citygml.cgml.CreateFromDocument(xml, location_base=xml_path) except pyxb.ValidationError as e: print(e.details()) return cgml_model
rxml = v.childNodes[0].childNodes[0].value # Save the extracted response open('resp.xml', 'w').write(rxml) #rxml = open('resp.xml').read() # Create the binding instance from the response. If there's a # problem, diagnose the issue, then try again with validation # disabled. r = None try: r = DWML.CreateFromDocument(rxml) except pyxb.UnrecognizedContentError as e: print('*** ERROR validating response:') print(e.details()) if r is None: pyxb.RequireValidWhenParsing(False) r = DWML.CreateFromDocument(rxml) # Start spitting out the processed data. product = r.head.product print('%s %s' % (product.title, product.category)) source = r.head.source print(", ".join(source.production_center.content())) data = r.data[0] for i in range(len(data.location)): loc = data.location[i] print('%s [%s %s]' % (loc.location_key, loc.point.latitude, loc.point.longitude)) for p in data.parameters: if p.applicable_location != loc.location_key: