def to_xml(self): """Create an XML representation of an OTRS Object. @returns am etree.Element """ root = etree.Element(self.XML_NAME) for k, v in self.attrs.items(): e = etree.Element(k) if isinstance(e, str): v = v.encode('utf-8') if sys.version_info[0] == 3: e.text = str(v) else: e.text = unicode(v) root.append(e) return root
def __init__(self, raw_data: tuple, output_file: str): self.raw_data = raw_data self.output_file = output_file self.output_file = self.process_xml_extension(self.output_file) root = ET.Element("deck", attrib={"name": output_file[:-4]}) for data in raw_data: card = ET.Element("card") id_ = ET.SubElement(card, "id") id_.text = str(data[0]) front = ET.SubElement(card, "front") front.text = data[1] back = ET.SubElement(card, 'back') back.text = data[2] root.append(card) buf = BytesIO() buf.write(ET.tostring(root)) buf.seek(0) string = minidom.parse(buf).toprettyxml(indent=" " * 4) with open(self.output_file, mode="w+", encoding="utf-8") as file_: file_.write(string)
def queryAlerts(server, index, maxAlerts): xml = """{ "sort": { "createTime": { "order": "desc" } } }""" returnData = "" es = Elasticsearch() res = es.search(index=index, doc_type="Alert", body={"query": { "match_all": {} }}) print("Got %d Hits:" % res['hits']['total']) EWSSimpleAlertInfo = xmlParser.Element('EWSSimpleAlertInfo') alerts = xmlParser.SubElement(EWSSimpleAlertInfo, "Alerts") for hit in res['hits']['hits']: requestString = "%(originalRequestString)s " % hit["_source"] print("%(originalRequestString)s " % hit["_source"]) alert = xmlParser.SubElement(alerts, "Alert") requestXML = xmlParser.SubElement(alert, "Request") requestXML.text = requestString returnData = tostring(EWSSimpleAlertInfo) return returnData
def req(self, reqname, *args, **kwargs): """Wrapper around a SOAP request. @param reqname: the SOAP name of the request @param kwargs : to define the tags included in the request. @return : the full etree.Element of the response keyword arguments can be either - simple types (they'l be converted to <name>value</name>) - `OTRSObject`, they will be serialized with their `.to_xml()` - list of `OTRSObject`s: each `OTRSObject`s in the list will be serialized with their `.to_xml()` (used for dynamic fields and attachments). - list of simple types will be converted to multiple <name>value</name> elements (e.g. used for search filters) """ xml_req_root = etree.Element(reqname) for k, v in kwargs.items(): if isinstance(v, OTRSObject): e = v.to_xml() xml_req_root.append(e) elif isinstance(v, (list, tuple)): for vv in v: if isinstance(vv, OTRSObject): e = vv.to_xml() else: e = etree.Element(k) e.text = unicode(vv) xml_req_root.append(e) else: e = etree.Element(k) e.text = unicode(v) xml_req_root.append(e) request = urllib2.Request(self.endpoint, self._pack_req(xml_req_root), {'Content-Type': 'text/xml;charset=utf-8'}) try: if ((sys.version_info[0] == 3 and sys.version_info < (3, 4, 3)) or (sys.version_info < (2, 7, 9))): fd = urllib2.urlopen(request) else: try: fd = urllib2.urlopen(request, context=self.ssl_context) except TypeError: fd = urllib2.urlopen(request) except httplib.BadStatusLine: raise BadStatusLineError(request.get_full_url()) if fd.getcode() != 200: raise OTRSError(fd) else: try: s = fd.read() e = etree.fromstring(s) unpacked = self._unpack_resp_several(e) if (len(unpacked) > 0) and (unpacked[0].tag.endswith('Error')): raise SOAPError(unpacked[0]) return e except etree.ParseError: print('error parsing:') print('-' * 80) print(s) print('-' * 80) raise