如何以 XML 格式保存 top 命令的输出?

如何以 XML 格式保存 top 命令的输出?
top -o %MEM -b > file.txt

file.txt 如下:

top - 15:49:36 up 32 min,  2 users,  load average: 1.56, 1.40, 1.01
Tasks:  83 total,   2 running,  79 sleeping,   2 stopped,   0 zombie
%Cpu(s): 24.4 us,  8.1 sy,  0.0 ni, 55.0 id,  0.0 wa,  0.0 hi, 12.5 si,  0.0 st
KiB Mem:    195252 total,   191568 used,     3684 free,     3200 buffers
KiB Swap:   901116 total,       44 used,   901072 free.    66940 cached Mem

  PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND
  485 root      20   0   68160  61420   4552 R 58.7 31.5   5:06.10 python3
  712 www-data  20   0  230940   4436   2044 S 26.1  2.3   3:54.04 apache2
  819 root      20   0    8488   4400   3856 S  6.5  2.3   1:06.85 tcpdump
  713 www-data  20   0  230780   4344   2044 S 13.0  2.2   2:34.97 apache2
  868 root      20   0    6352   3976   2720 S  0.0  2.0   0:00.07 bash
  785 root      20   0    6340   3876   2636 S  0.0  2.0   0:00.06 bash
  707 root      20   0    6056   3704   2580 S  0.0  1.9   0:00.59 apache2
  137 root      20   0    8284   3256   2996 S  0.0  1.7   0:00.34 systemd-journal
    1 root      20   0    5476   3112   2072 S  0.0  1.6   0:01.04 systemd
  456 ntp       20   0    5820   3012   2576 S  0.0  1.5   0:00.71 ntpd
  865 root      20   0    4008   2792   2360 S  0.0  1.4   0:00.04 login
  938 nobody    20   0    5244   2756   2404 S  0.0  1.4   0:00.04 vsftpd
  941 nobody    20   0    5244   2756   2404 S  0.0  1.4   0:00.03 vsftpd
  457 root      20   0   31220   2740   2080 S  0.0  1.4   0:00.01 rsyslogd
  484 root      20   0    4008   2692   2268 S  0.0  1.4   0:00.05 login
  944 root      20   0    5076   2620   2352 R  0.0  1.3   0:00.00 top
  903 root      20   0    5080   2580   2236 T  0.0  1.3   0:00.00 top
  908 root      20   0    5052   2560   2288 T  0.0  1.3   0:00.08 top
  425 root      20   0    5416   2420   2100 S  0.0  1.2   0:00.00 cron
  406 root      20   0    5012   2348   2120 S  0.0  1.2   0:00.00 cron
  148 root      20   0   12272   2320   1856 S  0.0  1.2   0:00.09 systemd-udevd
  414 message+  20   0    5244   2268   1960 S  0.0  1.2   0:00.00 dbus-daemon
  390 statd     20   0    4576   2264   1764 S  0.0  1.2   0:00.00 rpc.statd

我应该以 XML 格式而不是 txt 文件保存信息!该怎么做?

我想我可以使用 Python 但我不知道如何使用:

#coding:utf-8

from xml.dom.minidom import Document

doc = Document()
root = doc.createElement('InfoMemoria')

相关内容