XML_PP(1)

πŸ’… xml_pp(1) β€” the pretty-printer page, bestie edition ✨

okay so this is xml_pp, aka the xml pretty-printer. that's the whole name section, we ate that πŸŽ€


🌈 SYNOPSYS

xml_pp [options] [<files>]

πŸ¦‹ DESCRIPTION

it's an XML pretty printer, and it runs on XML::Twig πŸ’œ. that's the vibe. it takes your crusty one-line XML and makes it look ✨iconic✨.

πŸ‘‘ OPTIONS

  • -i[<extension>] β€” edits the file(s) in place, no cap. and if you hand it an extension (with no space between -i and the extension, okay? like -i.bak, not -i .bak, we don't do that) then the original file gets backed up with that extension 🫢

the rules for the extension are literally the same as Perl's (peep perldoc perlrun for the tea β˜•): if the extension has no * in it, it just gets appended to the original file name. if the extension has one or more * characters, then each * gets replaced with the current filename. slay.

  • -s <style> β€” the style to use for pretty printing. your options are: none, nsgmls, nice, indented, record, or record_c (go read the XML::Twig docs for the exact description of those styles, i'm not gonna pretend i can out-explain them). default is 'indented' πŸ’–
  • -p <tag(s)> β€” preserves white spaces in tags. you can use several -p options, or just quote the tags if you need more than one πŸŽ€
  • -e <encoding> β€” uses XML::Twig output_encoding (which is based on Text::Iconv or Unicode::Map8 and Unicode::String, iykyk) to set the output encoding. by default the original encoding is preserved, no notes.

if you use this option, the XML declaration gets updated β€” and created if there wasn't one already ✨

⚠️ make sure the encoding is supported by the parser you use, if you want to actually be able to process the pretty_printed file. like, XML::Parser does not support 'latin1' for example β€” you gotta use 'iso-8859-1'. don't ask me why, just trust me 😭
  • -l β€” loads the documents in memory instead of outputting them as they're being parsed. this prevents a bug (see BUGS below) but uses more memory. tradeoff city πŸ’€
  • -f <file> β€” reads the list of files to process from <file>, one per line πŸ“
  • -v β€” verbose, aka it lists the current file being processed. main character energy for your terminal 🌸
  • -- β€” stop argument processing. use this to process files that start with - (yes, those exist, we've all been there) πŸ™ˆ
  • -h β€” display help πŸŽ€

⭐ 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

πŸ’€ BUGS

so there's this one bug bestie. elements with mixed content that start with an embedded element get an extra \n. like, this:

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

will be output as:

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

not me crying over a stray newline 😭. using the -l option solves this bug (but uses more memory, as established 🫢)


🧚 TODO

update XML::Twig to use Encode with perl 5.8.0 β€” it's on the list, it's giving "i'll get to it" ✨

πŸ’œ AUTHOR

Michel Rodriguez <mirod@xmltwig.com> πŸ‘‘


and that's the whole page, babe 🌈 β€” brought to you by perl v5.42.0 and dated 2025-01-16, so you know exactly which era of slay this is from. mwah πŸ’…πŸ’–

πŸ“¦ where this page comes from