MSXML from C++ - pretty print / indent newly created documents

前端 未结 5 1242
陌清茗
陌清茗 2020-12-31 17:07

I\'m writing out XML files using the MSXML parser, with a wrapper I downloaded from here: http://www.codeproject.com/KB/XML/JW_CXml.aspx. Works great except that when I crea

5条回答
  •  -上瘾入骨i
    2020-12-31 17:42

    I've written a sed script a while back for basic xml indenting. You can use it as an external indenter if all else fails (save this to xmlindent.sed, and process your xml with sed -f xmlindent.sed ). You might need cygwin or some other posix environment to use it though.

    Here's the source:

    :a
    />/!N;s/\n/ /;ta
    s/  / /g;s/^ *//;s/  */ /g
    /^/!N;s/\n//;te
    s/-->/\n/;D;
    }
    /^<[?!][^>]*>/{
    H;x;s/\n//;s/>.*$/>/;p;bb
    }
    /^<\/[^>]*>/{
    H;x;s/\n//;s/>.*$/>/;s/^    //;p;bb
    }
    /^<[^>]*\/>/{
    H;x;s/\n//;s/>.*$/>/;p;bb
    }
    /^<[^>]*[^\/]>/{
    H;x;s/\n//;s/>.*$/>/;p;s/^/ /;bb
    }
    /]*>//;ba
    }
    

    Hrmp, tabs seem to be garbled... You can copy-waste from here instead: XML indenting with sed(1)

提交回复
热议问题