in reply to Indenting XML

A lot of nodes above are pointing out solutions using various modules. I'm just thinking about how to do this without using an existing formatter (if only for the intellectual exercise).

Keep a variable named $tab_count, intitilized to 0. Each time you see a valid begin tag, increment $tab_count, and print a newline followed by "\t" x $tab_count after the begin tag. Whenever you see a valid end tag, decrement $tab_count and print the same string as above.

----
Reinvent a rounder wheel.

Note: All code is untested, unless otherwise stated

Replies are listed 'Best First'.
Re: Re: Indenting XML
by mirod (Canon) on Feb 21, 2003 at 17:50 UTC

    It is a little more complex than this... what do you do with mixed content for example: how would you indent this:

    <ul><li><b>bold</b> statement indeed <i>hardburn</i></li></ul>

    Update: Oops! Sorry, I should have read the original question before getting all excited. XML::Simple doesn't do mixed content, so there should not be any in the XML anyway. So then, yes, indenting is a little easier. You still need to parse tags properly though, which is not as easy as it might look.