■ XML을 들여쓰는 방법을 보여준다.
▶ 예제 코드 (PY)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
from xml.etree.ElementTree import Element, SubElement, dump def indent(node, level = 0): i = "\n" + level * " " * 4 if len(node): if not node.text or not node.text.strip(): node.text = i + " " * 4 if not node.tail or not node.tail.strip(): node.tail = i for node in node: indent(node, level + 1) if not node.tail or not node.tail.strip(): node.tail = i else: if level and (not node.tail or not node.tail.strip()): node.tail = i noteElement = Element("note") noteElement.attrib["date"] = "20171219" SubElement(noteElement, "to" ).text = "Tove" SubElement(noteElement, "from" ).text = "Jani" SubElement(noteElement, "heading").text = "Reminder" SubElement(noteElement, "body" ).text = "Don't forget me this weekend!" indent(noteElement) dump(noteElement) """ <note date="20171219"> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note> """ |