ejklasdfjalsd;f
pretty_xml
This is pre-1.0 and probably unstable. Use at your own risk
I don't recommend using this unless you are sure a human will have to read your XML file.
Non-features:
-
pretty_xml
does not support namespaced nodes yet. - deletes all linebreaks from input
Use
pretty_xml
expects that three main parameters be passed:
-
block
: elements that should have internal and external linebreaks -
compact
: elements that should have external linebreaks -
inline
: elements that should have neither internal not external linebreaks
Additional parameters are:
-
preserve_whitespace
: retain standalone whitespace only text nodes within compact and inline elements -
tab
: specifies the kind of space that should be used for indenting the output.
Less common parameters:
-
close_tags
: a list of tags that need explicit closing tags in the output (as in HTML5,<a id="something"></a>
is correct, but a self-closing<a id="something"/>
will not be parsed correctly in all cases) -
control_chars
: options are:named
or:hex
for<
,>
, and&
as literal characters in output; defaults to:named
Use these parameters within a hash to initialize an instance of the PrettyXML::PrettyPrint
class. To prettyprint a document, pass a Nokogiri::XML::Document
or a string as an argument to the pp
method.
Example
doc = '<root> <block>
<p>
</p>
</block><p>stuff<i> </i></p>
<structure>
<div>
<p>yo yo<i/></p>
</div>
</structure></root>'
options = {
:block => %w(root block structure div),
:compact => %w(p),
:inline => %w(i),
:preserve_whitespace => true,
:delete_all_linebreaks => true,
:tab => ' '
}
puts PrettyXML::PrettyPrint.new(options).pp(doc)
returns
<?xml version="1.0"?>
<root>
<block>
<p> </p>
</block>
<p>stuff<i> </i></p>
<structure>
<div>
<p>yo yo<i/></p>
</div>
</structure>
</root>