fixed formatXML()
This commit is contained in:
		| @@ -23,7 +23,7 @@ def formatHTML(source: str, prettify: bool) -> str: | |||||||
|         return html.tostring(htmlDoc).decode().replace("\n", "").replace(">    ", ">") |         return html.tostring(htmlDoc).decode().replace("\n", "").replace(">    ", ">") | ||||||
|     return etree.tostring(htmlDoc, encoding='unicode', pretty_print=True) |     return etree.tostring(htmlDoc, encoding='unicode', pretty_print=True) | ||||||
|  |  | ||||||
| def formatXML(source: str) -> str: | def formatXML(source: str, prettify: bool) -> str: | ||||||
|     """Method used to format XML |     """Method used to format XML | ||||||
|  |  | ||||||
|     :param source: XML to format |     :param source: XML to format | ||||||
| @@ -40,14 +40,20 @@ def formatXML(source: str) -> str: | |||||||
|     if prolog_start != -1: |     if prolog_start != -1: | ||||||
|         prolog_end = source.find("?>") + 2 |         prolog_end = source.find("?>") + 2 | ||||||
|         prolog = source[prolog_start:prolog_end] |         prolog = source[prolog_start:prolog_end] | ||||||
|         source = source[prolog_end: ] |         source = source[prolog_end:].strip() | ||||||
|  |  | ||||||
|     byte_input = BytesIO(source.encode("utf-8")) |     byte_input = BytesIO(source.encode("utf-8")) | ||||||
|     parser = etree.XMLParser(remove_blank_text=True) |     parser = etree.XMLParser(remove_blank_text=True) | ||||||
|     xml = etree.parse(byte_input, parser=parser) |     xml = etree.parse(byte_input, parser=parser) | ||||||
|  |  | ||||||
|     return prolog + etree.tostring(xml, pretty_print=False).decode() |     if prettify: | ||||||
|  |         prolog += "\n" | ||||||
|  |         return prolog + etree.tostring(xml, pretty_print=True, encoding="utf-8").decode("utf-8") | ||||||
|  |  | ||||||
|  |     raw_xml = etree.tostring(xml, encoding="utf-8").decode("utf-8") | ||||||
|  |     raw_xml = " ".join(raw_xml.split()) | ||||||
|  |  | ||||||
|  |     return prolog + raw_xml | ||||||
|  |  | ||||||
| def xpath(source: str, xpath: str) -> str: | def xpath(source: str, xpath: str) -> str: | ||||||
|     """ |     """ | ||||||
| @@ -80,7 +86,6 @@ def xpath(source: str, xpath: str) -> str: | |||||||
|         return result_string, "node" |         return result_string, "node" | ||||||
|  |  | ||||||
|  |  | ||||||
|  |  | ||||||
| def xsd(source: str, xsd: str) -> bool: | def xsd(source: str, xsd: str) -> bool: | ||||||
|     """ |     """ | ||||||
|     Method used to validate XML string against XSD schema |     Method used to validate XML string against XSD schema | ||||||
| @@ -101,8 +106,6 @@ def xsd(source: str, xsd: str) -> bool: | |||||||
|     except etree.DocumentInvalid as e: |     except etree.DocumentInvalid as e: | ||||||
|         return str(e) |         return str(e) | ||||||
|  |  | ||||||
|      |  | ||||||
|      |  | ||||||
| def xslt(source: str, xslt: str) -> str: | def xslt(source: str, xslt: str) -> str: | ||||||
|     """ |     """ | ||||||
|     Method used to transform XML string using XSLT |     Method used to transform XML string using XSLT | ||||||
|   | |||||||
| @@ -39,7 +39,7 @@ def process_xml(request: request, type: str) -> str: | |||||||
|         # elif (type == "prettify"): |         # elif (type == "prettify"): | ||||||
|         #     response_json['result'] = Parser.formatXML(data, True) |         #     response_json['result'] = Parser.formatXML(data, True) | ||||||
|         elif (type == "minimize"): |         elif (type == "minimize"): | ||||||
|             response_json['result'] = Parser.formatXML(data) |             response_json['result'] = Parser.formatXML(data, False) | ||||||
|         elif (type == "prettifyHtml"): |         elif (type == "prettifyHtml"): | ||||||
|             response_json['result'] = Parser.formatHTML(data, True) |             response_json['result'] = Parser.formatHTML(data, True) | ||||||
|         elif (type == "minimizeHtml"): |         elif (type == "minimizeHtml"): | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user