| 61 | |
| 62 | |
| 63 | def PrettyPrintNode(node, indent=0): |
| 64 | if node.nodeType == Node.TEXT_NODE: |
| 65 | if node.data.strip(): |
| 66 | print("{}{}".format(" " * indent, node.data.strip())) |
| 67 | return |
| 68 | |
| 69 | if node.childNodes: |
| 70 | node.normalize() |
| 71 | # Get the number of attributes |
| 72 | attr_count = 0 |
| 73 | if node.attributes: |
| 74 | attr_count = node.attributes.length |
| 75 | |
| 76 | # Print the main tag |
| 77 | if attr_count == 0: |
| 78 | print("{}<{}>".format(" " * indent, node.nodeName)) |
| 79 | else: |
| 80 | print("{}<{}".format(" " * indent, node.nodeName)) |
| 81 | |
| 82 | all_attributes = [] |
| 83 | for name, value in node.attributes.items(): |
| 84 | all_attributes.append((name, value)) |
| 85 | all_attributes.sort(CmpTuple()) |
| 86 | for name, value in all_attributes: |
| 87 | print('{} {}="{}"'.format(" " * indent, name, value)) |
| 88 | print("%s>" % (" " * indent)) |
| 89 | if node.nodeValue: |
| 90 | print("{} {}".format(" " * indent, node.nodeValue)) |
| 91 | |
| 92 | for sub_node in node.childNodes: |
| 93 | PrettyPrintNode(sub_node, indent=indent + 2) |
| 94 | print("{}</{}>".format(" " * indent, node.nodeName)) |
| 95 | |
| 96 | |
| 97 | def FlattenFilter(node): |