2

I have a log file with lots of web service XML data (example follows). Ideally I would like to just go to a line and type ,x to create pretty formatted XML for readability.

 Response XML: <ZZZZZ_EXBListLocalEftposRS xmlns="http://types.wise.zzzzz.com/2005/06"><PayLoad><TimeStamp>2015-04-07T10:25:18.797+12:00</TimeStamp></PayLoad><Success/><EftposTerminalID CurrentlyMapped="true" ProbablyLocal="true">TEST_EFTD</EftposTerminalID><EftposTerminalID CurrentlyMapped="true" ProbablyLocal="true">TEST_EFT</EftposTerminalID><EftposTerminalID CurrentlyMapped="false" ProbablyLocal="false">TESTBadD1</EftposTerminalID><EftposTerminalID CurrentlyMapped="false" ProbablyLocal="false">TESTBadD2</EftposTerminalID><EftposTerminalID CurrentlyMapped="false" ProbablyLocal="false">TESTBadD3</EftposTerminalID><EftposTerminalID CurrentlyMapped="false" ProbablyLocal="false">TESTBadNoP</EftposTerminalID><EftposTerminalID CurrentlyMapped="false" ProbablyLocal="false">TESTBadS</EftposTerminalID><EftposTerminalID CurrentlyMapped="false" ProbablyLocal="false">TESTBadS2</EftposTerminalID><EftposTerminalID CurrentlyMapped="false" ProbablyLocal="false">TESTBadS3</EftposTerminalID><EftposTerminalID CurrentlyMapped="false" ProbablyLocal="false">TESTBadS4</EftposTerminalID><EftposTerminalID CurrentlyMapped="false" ProbablyLocal="false">TESTBadS5</EftposTerminalID><EftposTerminalID CurrentlyMapped="false" ProbablyLocal="false">TESTBadS6</EftposTerminalID><EftposTerminalID CurrentlyMapped="false" ProbablyLocal="true">TESTBadScl</EftposTerminalID><EftposTerminalID CurrentlyMapped="false" ProbablyLocal="false">TESTBadTyp</EftposTerminalID></ZZZZZ_EXBListLocalEftposRS>

Currently, I can format the line as follows

  1. put the XML stuff on a new line (If you don't do this xmllint has problems)
  2. get the line number I need (199 in this case)
  3. call xmllint.exe as follows:

    :199 :!C:\apps\xmllint.exe % --format

Is there some way to automate some or all of this process?

I'm using gvim on windows, and xmllint.exe from google code.

2 Answers 2

2

You already have most of the pieces:

:199 :!C:\apps\xmllint.exe % --format

You can use :. for the current line:

:.!C:\apps\xmllint.exe % --format

To get rid of the preamble before the XML, you can use 0dt<. Inside a command-line, that's done with :normal!:

:execute 'normal! 0dt<' | .!C:\apps\xmllint.exe % --format

Now, all that is left is creating a mapping (note how some keys need a special format):

nnoremap ,x :execute 'normal! 0dt<lt>' <Bar> .!C:\apps\xmllint.exe % --format<CR>
1

Another option would be to install Syntastic. It has support for xml lint testing using xmllint. This will do the checking and report errors and not directly fix the problem with the XML however.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.