Name

format()

Examples
# The following short XML file called "mammals.xml" is parsed 
# in the code below. It must be in the project's "data" folder.
#
# <?xml version="1.0"?>
# <mammals>
# <animal id="0"species="Capra hircus">Goat</animal>
# <animal id="1"species="Panthera pardus">Leopard</animal>
# <animal id="2"species="Equus zebra">Zebra</animal>
# </mammals>
xml = None
def setup(): 
 xml = loadXML("mammals.xml")
 
 #Format without line breaks and no indentation
 s = xml.format(-1)
 print(s)
 print("")# Blank line
 
 #Format with line breaks and no indentation
 s = xml.format(0)
 print(s)
 
 #Format with line breaks and 5 spaces of indentation
 s = xml.format(5)
 print(s)
# Sketch prints:
#<mammals><animal id="0"species="Capra hircus">Goat</animal>
# <animal id="1"species="Panthera pardus">Leopard</animal>
# <animal id="2"species="Equus zebra">Zebra</animal></mammals>
#
#<?xml version="1.0"encoding="UTF-8"?>
#<mammals>
#<animal id="0"species="Capra hircus">Goat</animal>
#<animal id="1"species="Panthera pardus">Leopard</animal>
#<animal id="2"species="Equus zebra">Zebra</animal>
#</mammals>
#
#<?xml version="1.0"encoding="UTF-8"?>
#<mammals>
# <animal id="0"species="Capra hircus">Goat</animal>
# <animal id="1"species="Panthera pardus">Leopard</animal>
# <animal id="2"species="Equus zebra">Zebra</animal>
#</mammals>
Description Takes an XML object and converts it to a String, formatting its content as specified with the indent parameter.

If indent is set to -1, then the String is returned with no line breaks, no indentation, and no XML declaration.

If indent is set to 0 or greater, then the String is returned with line breaks, and the specified number of spaces as indent values. Meaning, there will be no indentation if 0 is specified, or each indent will be replaced with the corresponding number of spaces: 1, 2, 3, and so on.
Syntax
.format(indent)
Parameters
indentint: -1 for a single line (and no declaration), >= 0 for indents and newlines
Related toString()

Updated on Tue Feb 27 14:07:12 2024.

If you see any errors or have comments, please let us know.

Creative Commons License

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License

AltStyle によって変換されたページ (->オリジナル) /