fixed formatXML()

This commit is contained in:
2025-02-10 13:24:49 +01:00
parent 8797cac324
commit eb0e895eab
2 changed files with 11 additions and 8 deletions

View File

@@ -23,7 +23,7 @@ def formatHTML(source: str, prettify: bool) -> str:
return html.tostring(htmlDoc).decode().replace("\n", "").replace("> ", ">")
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
:param source: XML to format
@@ -40,14 +40,20 @@ def formatXML(source: str) -> str:
if prolog_start != -1:
prolog_end = source.find("?>") + 2
prolog = source[prolog_start:prolog_end]
source = source[prolog_end: ]
source = source[prolog_end:].strip()
byte_input = BytesIO(source.encode("utf-8"))
parser = etree.XMLParser(remove_blank_text=True)
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:
"""
@@ -80,7 +86,6 @@ def xpath(source: str, xpath: str) -> str:
return result_string, "node"
def xsd(source: str, xsd: str) -> bool:
"""
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:
return str(e)
def xslt(source: str, xslt: str) -> str:
"""
Method used to transform XML string using XSLT

View File

@@ -39,7 +39,7 @@ def process_xml(request: request, type: str) -> str:
# elif (type == "prettify"):
# response_json['result'] = Parser.formatXML(data, True)
elif (type == "minimize"):
response_json['result'] = Parser.formatXML(data)
response_json['result'] = Parser.formatXML(data, False)
elif (type == "prettifyHtml"):
response_json['result'] = Parser.formatHTML(data, True)
elif (type == "minimizeHtml"):