Provide finer control over indentation inside the XMLPrinter. You'll have to subclass it to override its standard behaviour by overwriting CompactMode().
diff --git a/tinyxml2.cpp b/tinyxml2.cpp
index 4e79ef5..2ecb50a 100755
--- a/tinyxml2.cpp
+++ b/tinyxml2.cpp
@@ -1940,17 +1940,17 @@
}
-void XMLPrinter::OpenElement( const char* name )
+void XMLPrinter::OpenElement( const char* name, bool compactMode )
{
if ( _elementJustOpened ) {
SealElement();
}
_stack.Push( name );
- if ( _textDepth < 0 && !_firstElement && !_compactMode ) {
+ if ( _textDepth < 0 && !_firstElement && !compactMode ) {
Print( "\n" );
}
- if ( !_compactMode ) {
+ if ( !compactMode ) {
PrintSpace( _depth );
}
@@ -2002,7 +2002,7 @@
}
-void XMLPrinter::CloseElement()
+void XMLPrinter::CloseElement( bool compactMode )
{
--_depth;
const char* name = _stack.Pop();
@@ -2011,7 +2011,7 @@
Print( "/>" );
}
else {
- if ( _textDepth < 0 && !_compactMode) {
+ if ( _textDepth < 0 && !compactMode) {
Print( "\n" );
PrintSpace( _depth );
}
@@ -2021,7 +2021,7 @@
if ( _textDepth == _depth ) {
_textDepth = -1;
}
- if ( _depth == 0 && !_compactMode) {
+ if ( _depth == 0 && !compactMode) {
Print( "\n" );
}
_elementJustOpened = false;
@@ -2146,7 +2146,9 @@
bool XMLPrinter::VisitEnter( const XMLElement& element, const XMLAttribute* attribute )
{
- OpenElement( element.Name() );
+ const XMLElement* parentElem = element.Parent()->ToElement();
+ bool compactMode = parentElem ? CompactMode(*parentElem) : _compactMode;
+ OpenElement( element.Name(), compactMode );
while ( attribute ) {
PushAttribute( attribute->Name(), attribute->Value() );
attribute = attribute->Next();
@@ -2155,9 +2157,9 @@
}
-bool XMLPrinter::VisitExit( const XMLElement& )
+bool XMLPrinter::VisitExit( const XMLElement& element )
{
- CloseElement();
+ CloseElement( CompactMode(element) );
return true;
}
diff --git a/tinyxml2.h b/tinyxml2.h
index fc54320..605a1ca 100755
--- a/tinyxml2.h
+++ b/tinyxml2.h
@@ -1967,7 +1967,7 @@
/** If streaming, start writing an element.
The element must be closed with CloseElement()
*/
- void OpenElement( const char* name );
+ void OpenElement( const char* name, bool compactMode );
/// If streaming, add an attribute to an open element.
void PushAttribute( const char* name, const char* value );
void PushAttribute( const char* name, int value );
@@ -1975,7 +1975,7 @@
void PushAttribute( const char* name, bool value );
void PushAttribute( const char* name, double value );
/// If streaming, close the Element.
- virtual void CloseElement();
+ virtual void CloseElement( bool compactMode );
/// Add a text node.
void PushText( const char* text, bool cdata=false );
@@ -2034,6 +2034,8 @@
}
protected:
+ virtual bool CompactMode( const XMLElement& elem ) { return _compactMode; };
+
/** Prints out the space before an element. You may override to change
the space and tabs used. A PrintSpace() override should call Print().
*/