Click here to Skip to main content
15,881,821 members
Articles / Programming Languages / C++
Article

Reformatting XML

Rate me:
Please Sign up or sign in to vote.
4.00/5 (1 vote)
9 Apr 2001 63.1K   672   7   5
A Developer Studio macro to reformat XML
  • Download source - 2 Kb
  • So, it's a weekend, I'm hunting for a Developer Studio macro that will format XML, and I come across Alvaro Mendez's nice little C/C++ formatting macro (MakeCodeNicer). I spend a few hours whittling it down to what I need for XML and voila! I now have an XML formatter (MakeXMLNicer).

    If you do a lot of work with Microsoft's XML parser (particularly using it to write out XML), you've probably noticed you can't tell the parser to insert white space for new elements. So you end up with XML tags lined up in a nice long string. Makes it really hard to follow the XML structure.

    The macro will handle the main <?xml?> tag, comment tags, normal start/end element tags and the special single element tags (<foo/>). Indention is handled by inserting tabs although you could change this pretty easily to spaces if you prefer. Any existing indention or new line breaks are removed.

    Attribute values are dealt with in a special way. Any embedded new lines in the value are stripped except when the attribute value exceeds 1000 characters - DevStudio can't handle very long lines and I've run into XML with very long base64-encoded attribute values. In these cases, the attribute values are left alone.

    Element text (the text between a start tag and end tag) is completely left alone - no tabs, new lines, etc are removed since it's impossible to know if these might be important to the context of the element. Some XML might use tabs in element values for a special meaning - stripping them would damage the XML.

    BTW, a thanks goes to Alvaro for the inspiration and some macro code to save me some time.

    Enjoy.

    Fixes:

    • 3 Apr 2001: Fixed problem w/ removing spaces in XML element values.

    Known Problems:

    • Element (tag) names that are "broken" by new lines are not handled. These would show up as names with a single embedded space to replace the new line. This would "break" the XML, but should be a very rare occurrence.
    • The nasty DevStudio "Paste" bug (where Paste stops working) can occur with macros that use the ActiveDocument.Selection method to assign replacement values. I minimized use of this method as much as possible to reduce the likelihood you will encounter this problem, but it's still there. Unfortunately, Joshua Jensen's excellent PasteFix add-in won't solve this problem since ActiveDocument.Selection is using clipboard functions directly and not going thru the Ctrl-V keyboard shortcut. But I'd heartily recommend that you get Joshua's add-in to eliminate the problem everywhere else.
    • The macro will reformat the entire file - there is no support for reformatting a selection yet.

    License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here


    Written By
    United States United States
    This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

    Comments and Discussions

     
    GeneralPaste bug Pin
    Stanislav Volodarskiy29-Jun-02 21:43
    Stanislav Volodarskiy29-Jun-02 21:43 
    GeneralWhite spaces Pin
    Juraj Rojko10-Apr-01 22:42
    Juraj Rojko10-Apr-01 22:42 
    GeneralSpaces not handled properly Pin
    3-Apr-01 10:11
    suss3-Apr-01 10:11 
    GeneralRe: Spaces not handled properly Pin
    3-Apr-01 10:16
    suss3-Apr-01 10:16 
    GeneralRe: Spaces not handled properly Pin
    Mark Baker5-Apr-01 8:34
    Mark Baker5-Apr-01 8:34 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.