Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Indenting XML

by hardburn (Abbot)
on Feb 21, 2003 at 17:30 UTC ( [id://237499]=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://237499]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2024-04-24 10:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found