Friday, February 14, 2014

NERD ALERT: Using Vim to convert an XML statement into 1 tag per line

I'm currently working on a task to document some integration messages. They are in a text file, all on one line, and I need to add them to a Word doc.


Oh, and all the tags need to be on their own line. Example:

<?xml version='1.0' encoding='UTF-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><get_stuff ><user><now>2014-02-10T09:42:04-07:00</now><company>fieldserviceisnothard</company><login>yermom</login><auth_string>80fa892c2facKe0bp3n1Se6cdc2f</auth_string></user></get_stuff></soapenv:Body></soapenv:Envelope>

This SUCKS.

Or at least it did. I downloaded Vim for Windows.  I used this search and replace pattern:


:%s/></>\r</g

Translation:
:%s/ = search the document from start to finish, not just this line
></ = the string I want to find. It's the end of one tag and the beginning of another.
 >\r< = what I want to change it to. \r is a new line.
/g = Global search (within the selected line). I think. I'm not that good with Vi/Vim/Unix text editors. Oh well: worked like a boss.

And now my output is instantly:

<?xml version='1.0' encoding='UTF-8'?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<get_stuff >
<user>
<now>2014-02-10T09:42:04-07:00</now>
<company>fieldserviceisnothard</company>
<login>yermom</login>
<auth_string>80fa892c2facKe0bp3n1Se6cdc2f</auth_string>
</user>
</get_stuff>
</soapenv:Body>
</soapenv:Envelope>



Hooray Nerd Alert!

No comments:

Post a Comment