XML_PP(1p)

πŸ’– xml_pp β€” the pretty-printer era πŸ’–

okay bestie, so this is XML_PP(1p), from the User Contributed Perl Documentation section, and it's literally the glow-up your ugly XML has been begging for. no cap. πŸ¦‹


✨ NAME ✨

xml_pp β€” xml pretty-printer. that's it, that's the name, and it ate. πŸ’…


✨ SYNOPSYS ✨

xml_pp [options] [<files>]

(yes the section is spelled like that, i'm not fixing it, it's iconic πŸ™ˆ)


✨ DESCRIPTION ✨

so basically? XML pretty printer using XML::Twig. it takes your crusty unreadable XML and makes it serve main character energy ✨


✨ OPTIONS ✨

  • -i[<extension>] β€” edits the file(s) in place 🫢. if you give it an extension (and there is no space between -i and the extension, okay? none. zero. zip.) then the original file gets backed-up with that extension. the rules for the extension are the same as Perl's (see perldoc perlrun): if the extension includes no * then it is appended to the original file name; if the extension does contain one or more * characters, then each * is replaced with the current filename. slay.
  • -s <style> β€” the style to use for pretty printing: none, nsgmls, nice, indented, record, or record_c (go check the XML::Twig docs for the exact description of those styles, i'm just the messenger πŸ’œ). default is indented, because of course it is.
  • -p <tag(s)> β€” preserves white spaces in tags. you can stack several -p options or just quote the tags if you need more than one. whatever vibes, bestie πŸŽ€
  • -e <encoding> β€” use XML::Twig output_encoding (based on Text::Iconv or Unicode::Map8 and Unicode::String) to set the output encoding. by default the original encoding is preserved, no notes. if you use this option the XML declaration is updated (and created if there was none) ✨
make sure that the encoding is supported by the parser you use if you want to be able to process the pretty_printed file (XML::Parser does not support latin1 for example, you have to use iso-8859-1). fr fr, this one bites people.
  • -l β€” loads the documents in memory instead of outputting them as they are being parsed. this prevents a bug (see BUGS) but uses more memory. trade-offs, babe πŸ’€
  • -f <file> β€” read the list of files to process from <file>, one per line πŸ“‹
  • -v β€” verbose (list the current file being processed). she's chatty and we love her πŸ—£οΈ
  • -- β€” stop argument processing (to process files that start with -) πŸ›‘
  • -h β€” display help. bestie behaviour πŸ™‹β€β™€οΈ

🌈 EXAMPLES 🌈

xml_pp foo.xml > foo_pp.xml           # pretty print foo.xml
xml_pp < foo.xml > foo_pp.xml         # pretty print from standard input

xml_pp -v -i.bak *.xml                # pretty print .xml files, with backups
xml_pp -v -i'orig_*' *.xml            # backups are named orig_<filename>

xml_pp -i -p pre foo.xhtml            # preserve spaces in pre tags

xml_pp -i.bak -p 'pre code' foo.xml   # preserve spaces in pre and code tags
xml_pp -i.bak -p pre -p code foo.xml  # same

xml_pp -i -s record mydb_export.xml   # pretty print using the record style

xml_pp -e utf8 -i foo.xml             # output will be in utf8
xml_pp -e iso-8859-1 -i foo.xml       # output will be in iso-8859-1

xml_pp -v -i.bak -f lof               # pretty print in place files from lof

xml_pp -- -i.xml                      # pretty print the -i.xml file

xml_pp -l foo.xml                     # loads the entire file in memory
                                      # before pretty printing it

xml_pp -h                             # display help

look at her go πŸ˜­πŸ‘‘ every single one of these is a serve.


πŸ’€ BUGS πŸ’€

okay so here's the tea. elements with mixed content that start with an embedded element get an extra \n. not me exposing the flawβ€”

<elt><b>b</b>toto<b>bold</b></elt>

will be output as

<elt>
  <b>b</b>toto<b>bold</b></elt>

using the -l option solves this bug (but uses more memory). so like, pick your struggle 🫠


πŸ“ TODO πŸ“

  • update XML::Twig to use Encode with perl 5.8.0 β€” manifesting this one for her πŸ™βœ¨

πŸ‘‘ AUTHOR πŸ‘‘

Michel Rodriguez <mirod@xmltwig.com> β€” the icon, the legend, the moment πŸ’œ


perl v5.40.1 πŸ¦‹ 2025-12-18 πŸŽ€ β€” and that's the whole page bestie, go forth and pretty-print, you're gonna eat πŸ’…βœ¨πŸ«Ά

πŸ“¦ where this page comes from