Click here to Skip to main content
Click here to Skip to main content
Go to top

Reformatting XML

, 9 Apr 2001
Rate this:
Please Sign up or sign in to vote.
A Developer Studio macro to reformat XML
<!-- Download Links -->
  • Download source - 2 Kb
  • <!-- Main HTML starts here -->

    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

    Share

    About the Author

    Mark M. Baker

    United States United States
    No Biography provided

    Comments and Discussions

     
    GeneralPaste bug PinmemberStanislav Volodarskiy29-Jun-02 21:43 
    GeneralWhite spaces PinmemberJuraj Rojko10-Apr-01 22:42 
    GeneralSpaces not handled properly PinmemberAnonymous3-Apr-01 10:11 
    The following xml block is not handled correctly.
     

    This xml document get corrupted by MakeXMLNicer.After running the macro, there are no spaces in this text.

    GeneralRe: Spaces not handled properly PinmemberAnonymous3-Apr-01 10:16 
    GeneralRe: Spaces not handled properly PinmemberMark Baker5-Apr-01 8:34 

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

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

    | Advertise | Privacy | Mobile
    Web01 | 2.8.140921.1 | Last Updated 10 Apr 2001
    Article Copyright 2001 by Mark M. Baker
    Everything else Copyright © CodeProject, 1999-2014
    Terms of Service
    Layout: fixed | fluid