MCPcopy
hub / github.com/httpie/cli / pretty_xml

Function pretty_xml

httpie/output/formatters/xml.py:29–50  ·  view source on GitHub ↗

Render the given :class:`~xml.dom.minidom.Document` `document` into a prettified string.

(document: 'Document',
               declaration: Optional[str] = None,
               encoding: Optional[str] = UTF8,
               indent: int = 2)

Source from the content-addressed store, hash-verified

27
28
29def pretty_xml(document: 'Document',
30 declaration: Optional[str] = None,
31 encoding: Optional[str] = UTF8,
32 indent: int = 2) -> str:
33 """Render the given :class:`~xml.dom.minidom.Document` `document` into a prettified string."""
34 kwargs = {
35 'encoding': encoding or UTF8,
36 'indent': ' ' * indent,
37 }
38 body = document.toprettyxml(**kwargs).decode(kwargs['encoding'])
39
40 # Remove blank lines automatically added by `toprettyxml()`.
41 lines = [line for line in body.splitlines() if line.strip()]
42
43 # xml.dom automatically adds the declaration, even if
44 # it is not present in the actual body. Remove it.
45 if len(lines) >= 1 and parse_declaration(lines[0]):
46 lines.pop(0)
47 if declaration:
48 lines.insert(0, declaration)
49
50 return '\n'.join(lines)
51
52
53class XMLFormatter(FormatterPlugin):

Callers 2

__init__.pyFile · 0.90
format_bodyMethod · 0.85

Calls 2

parse_declarationFunction · 0.85
decodeMethod · 0.80

Tested by

no test coverage detected