NAME
XML::LibXML::PrettyPrint - add pleasant whitespace to a DOM tree
SYNOPSIS
my $document = XML::LibXML->new->parse_file('in.xml');
my $pp = XML::LibXML::PrettyPrint->new(indent_string => " ");
$pp->pretty_print($document); # modified in-place
print $document->toString;
DESCRIPTION
Long XML files can be daunting for humans to read. Of course, XML is
really designed for computers to read - not people - but there are times
when mere mortals do need to read and edit XML by hand. For example, if
your application stores its configuration in XML, or you need to dump some
XML to STDOUT for debugging purposes.
Syntax highlighting helps, but to really make sense of some XML, proper
indentation can be vital. Hence `XML::LibXML::PrettyPrint` - it can be
applied to an XML::LibXML DOM tree to reformat it into a more readable
result.
Pretty-printing XML is not as CPU-efficient as dumping it out sloppily, so
unless you're pretty sure that a human is going to need to make sense of
your XML, you should probably not use this module.
Constructors
`new(%options)`
Constructs a pretty-printer object.
Options:
* indent_string - The string to use to indent each line. Defaults to
a single tab character. Setting it to a non-whitespace character
is allowed, but will carp a warning.
* new_line - The string to use to begin a new line. Defaults to
"\n".
* element - A hashref of element categorisations. Each
categorisation is a reference to an array of element names or
callback functions. Element names may use Clark notation.
my $callback = sub {
my $node = shift;
return 1 if $node->hasAttribute('is_block');
return undef;
};
my $pp = XML::LibXML::PrettyPrint->new(
element => {
inline => [qw/span strong em b i a/],
block => [qw/p div body html head/, $callback],
compact => [qw/title caption li dd dt th td/],
preserves_whitespace => [qw/pre script style/],
}
);
Callbacks should return 1 (true), 0 (false) or undef (dunno).
`new_for_html(%options)`
Constructs a pretty printer object pre-configured to be suitable for
HTML and XHTML. The indent_string and new_line options are supported.
Methods
If you just need to use a default configuration (no options passed to the
constructor, then you can call these as class methods, unless otherwise
stated.
`strip_whitespace($node)`
Strips superfluous whitespace from an `XML::LibXML::Document` or
`XML::LibXML::Element`.
Whitespace just before, just after or leading/trailing within an
inline element is not considered superfluous. Runs of multiple
whitespace characters are replaced with a single space. Whitespace is
not changed within an element that preserves whitespace.
The node is modified in place.
`indent($node, $level)`
Indents the node to a certain indentation level, and its direct
children to `$level + 1`, grandchildren to `$level + 2`, etc.
Typically you'd just want to indent the root node to level 0.
The node is modified in place.
Elements that preserve whitespace are not changed.
`pretty_print($node, $level)`
Strip whitespace and indent. The node is modified in place and
returned.
Example use as a class method:
print XML::LibXML::PrettyPrint
->pretty_print(XML::LibXML->new->parse_string($XML))
->toString;
`indent_string($level)`
Returns the string that would be used to indent something to a
particular level. Descendent classes could override this method to do
funky indentation, such as having varying levels of indentation.
`new_line`
Returns the string that would be used to begin a new line.
`element_category($node)`
Returns EL_INLINE, EL_BLOCK, EL_COMPACT or undef.
`element_preserves_whitespace($node)`
Boolean indicating whether the contents of the element have
significant whitespace that needs preserving.
Returns undef if $node is not an `XML::LibXML::Element`.
Functions
`print_xml $xml`
Given an XML string or an XML::LibXML::Node object, prints it nicely.
This function is not exported by default, but can be requested:
use XML::LibXML::PrettyPrint 0.001 qw(print_xml);
Use like this:
print_xml '