|
NAMEXML::LibXML::PrettyPrint - add pleasant whitespace to a DOM treeSYNOPSISmy $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; DESCRIPTIONLong 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
MethodsIf 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.
Functions
ConstantsThese can be exported:use XML::LibXML::PrettyPrint 0.001 qw(:constants);
ELEMENT CATEGORIESThere are three categories of element: inline, block and compact.For inline elements the presence of whitespace (though not the amount of whitespace) is considered significant just before the element, just after the element, or just within the element. In XHTML, consider the difference between the block element "<div>": <div>Will</div><div>Carlton</div> <div>Ashley</div> and the inline element "<span>": <span>Spider</span>-<span>Man</span> <span>lives</span> The space or lackthereof between "<div>" elements does not matter one whit. The lack of spaces between the first two "<span>" elements allows them to be read as a single (in this case, hyphenated) word, whereas the space before the third "<span>" separates out the word "lives". In terms of indentation, inline elements do not start a new indented line, unless they are the first element within their block, or are preceded by a block or compact element. Block elements always start a new line, and cause their child nodes to be indented to the next level. Compact elements are somewhere in-between. When it comes to whitespace stripping, they're treated as block elements. In terms of indentation, they always start a new line, but they only cause their child nodes to be indented to the next level if they have block descendents. If we imagine that in HTML, "<ul>" is a block element, "<i>" is an inline element, and "<li>" is a compact element: <ul> <li>Will Smith - Will Smith</li> <li>Carlton Banks - Alfonso Ribeiro</li> <li> Vivian Banks: <ul> <li>Janet Hubert-Whitten <i>(seasons 1-3)</i></li> <li>Daphne Maxwell Reid <i>(seasons 3-6)</i></li> </ul> </li> </ul> The third "<li>" element is indented like a block element because it contains a block "<ul>" element. The other "<li>" elements do not have their contents indented, because they contain only inline content. Elements default to being block, but you can specify particular elements as inline or compact by passing node names or callbacks to the constructor. Elements default to not preserving whitespace unless they have an "xml:space="preserve"" attribute, but again you can use the constructor to change this. Comments and processing instructions default to being compact, but you can make particular comments or PIs inline or block by passing appropriate callbacks to the constructor. Whitespace within comments and PIs is always preserved. (There is rarely any reason to make comments and processing instructions block, but making them inline can occasionally be useful, as it will mean that the presence of whitespace just before or just after the comment is treated as significant.) Text nodes are always inline. BUGSPlease report any bugs to <http://rt.cpan.org/Dist/Display.html?Queue=XML-LibXML-PrettyPrint>.SEE ALSORelated: XML::LibXML, HTML::HTML5::Writer.XML::Tidy - similar, but based on XML::XPath. Doesn't differentiate between inline and block elements. XML::Filter::Reindent - similar again, based on XML::Parser. Doesn't differentiate between inline and block elements. Sermon: <http://www.derkarl.org/why_to_tabs.html>. Read it. AUTHORToby Inkster <tobyink@cpan.org>.COPYRIGHT AND LICENCEThis software is copyright (c) 2011-2014 by Toby Inkster.This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. DISCLAIMER OF WARRANTIESTHIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
Visit the GSP FreeBSD Man Page Interface. |