The following example shows the usage of the xmlfromatter class: import xmlformatterįormatter = xmlformatter.Formatter(indent="1", indent_char="\t", encoding_output="ISO-8859-1", preserve=)įormatter.format_file("/home/pa/doc.xml") By default element content will be formatted everywhere - also within mixed content. Setting the boolean property inline to False suppresses inline formatting. Otherwise xmlformatter use the inpurt encoding. encoding_output advises xmlformatter to encode the output explicit by the given value. Without a value given to encoding_input xmlformatter trys to determine the encoding from the XML document. Setting the boolean property compress to True suppresses the indenting given by the indent and indent_char properties. All descendants of elements listed by preserve are left unformatted. By default all parts of the XML document will formatted. The Formatter class can be used to format XML documents in scripts. Class class xmlformatter.Formatter(compress ::= False, selfclose ::= False, indent ::= 2, indent_char ::= " ", inline ::= True, encoding_input ::= None, encoding_output ::= None, preserve ::=, blanks ::= False) The following output shows the formatted XML document: Įs war einmal und ist nicht mehr ein riesengroßer Teddybär, der aß die Milchund trank das Brot und als er starb da war er tot. Und als er starb da war erEEEEtot.CCCCAAAA The following example shows the described whitespaces by their labels within a XML document: AAAAĪAAABBBBEsDDDDwar einmal und istEEEEnicht mehrFĭer aßFFFFdie MilchEEEEund trank das BrotFFFF G: Linebreak and whitespace are used to indent elements within elements content. The formatting rules are:Ī: Surrounding whitespaces are removed from element content.ī: Leading whitespaces are removed from element content.Ĭ: Trailing whitespaces are removed from element content.ĭ: Leading whitespaces of inline elements are put to preceding text (or inserted) if necessary within mixed content.Į: Trailing whitespaces of inline elements are put to following text (or inserted) if necessary within mixed content.į: Sequences of whitespaces (n>2) are replaced by a single blank “ “ within element and mixed content. Es war einmal und ist nicht mehr.īoth styles are used while formatting a XML document. ![]() ![]() This formatting style is called text style. xmlfromatter has removed leading and trailing whitespaces and has collapsed sequences of whitespaces. The following output shows the formatted XML document. Even sequences of whitespaces will be collapsed: Es war einmal und ist nicht mehr. Note: xmlformatter may insert a text node if necessary. They will be adopted by the previous or following text node. Leading and trailing whitespaces enclosed by inline elements are misplaced. The inline element em gives a text snippet a special meaning. The outer element enclose poem encloses the text. Xmlformatter treats the mixed content from the following example as a literal text with some markup. This formatting style is called object style. xmlformatter has removed leading and trailing whitespaces from the text nodes and has indented the child elements equal. The element content from the example above can be formatted by xmlformat: $ xmlformat ele.xml Leading and trailing whitespaces will be remove, sequences of whitespaces will be collapsed. Leading and trailing whitespaces are meaningless in this scenario like sequences of whitespaces. Text nodes are associated with property values, like 4.4E+12. The elements are associated with containers, like complex, or properties names, like real and imaginary. Xmlformatter treats the element content from the following example as a object. ![]() In addition xmlformatter comes with a wrapper script named xmlformat. You may find xmlformatter useful for corrections and presentations. xmlformatter differs from others formatters by handling whitespaces by a distinct set of formatting rules - formatting element content by a object style and mixed content by a text style. It is the replacement for the Python 2 package XmlFormatter, which has been removed from PyPi completely (see Notes). Xmlformatter is an Open Source Python package, which provides formatting of XML documents.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |