尝试使用元素树将小于或大于等于的值打印在XML节点内作为值:
code:
messageValue = f"<![CDATA[{generate_MSH(i)}"
ET.SubElement(root, "Message", MsgID="1").text = messageValue
tree = ET.ElementTree(root)
tree.write("filename.xml", xml_declaration=True, encoding="UTF-8")
output: <Message MsgID="1"><![CDATA[MSH|^~\&|PATHL7
required output <Message MsgID="1"><![CDATA[MSH|^~\&;|PATHL7
XML文件的输出不打印<或&,而是打印<和&amp。