Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Huiu

macrumors member
Original poster
May 22, 2012
83
3
I have some old msn chat logs (.xml) that I'd like to read. I can open them with textwrangler (but not with word or excel or safari) but they're not readably organised. Which program is recommended to read them properly?
 
A chat log in XML should be very easy to parse with a short script or transform with XSLT to something more readable. But I suspect nobody will be able to help you without seeing the file format (I wasn't able to find anything online).
Do you have maybe a sample that doesn't contain anything sensitive? I'd need to see at least the part from the beginning to a point where it's just one message after another.
 
<?xml version="1.0"?>

<?xml-stylesheet type='text/xsl' href='MessageLog.xsl'?>

<Log FirstSessionID="1" LastSessionID="29"><Message Date="16.01.2005" Time="13:35:18" DateTime="2005-01-16T12:35:18.860Z" SessionID="1"><From><User FriendlyName="User1"/></From><To><User FriendlyName="User2"/></To><Text Style="font-family:Arial; font-weight:bold; color:#800000; ">aaa1</Text></Message><Message Date="16.01.2005" Time="13:35:23" DateTime="2005-01-16T12:35:23.898Z" SessionID="1"><From><User FriendlyName="User2"/></From><To><User FriendlyName="User1"/></To><Text Style="font-family:MS Shell Dlg; color:#000000; ">aaa2</Text></Message><Message Date="16.01.2005" Time="13:35:27" DateTime="2005-01-16T12:35:27.493Z" SessionID="1">


Is this sufficient?
 
Okay, this should do.
We'll transform your XML files through what's called XSLT. This would require you to install some software, but fortunately there are online converters that run your transformation for you.
Go to
Code:
https://www.freeformatter.com/xsl-transformer.html
Paste your XML file into the first field or upload it with the option below.
Then paste this into the XLS Input field:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/Log">
    <html>
      <body>
        <xsl:for-each select="Message">
          <div style="background-color: #cce6ff; border-radius: 1em; margin: 2em; padding: 1em">
            <p><strong>From:</strong> <xsl:value-of select="From/User/@FriendlyName"/> </p>
            <p><strong>To:</strong> <xsl:value-of select="To/User/@FriendlyName"/> </p>
            <xsl:value-of select="Text"/>
          </div>
        </xsl:for-each>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>
Then choose Tranform XML in new window. When the new window opens, just press Cmd+S to save the page. Do not add the .txt extension, we need a .html file. Open the file.


PS: This should work if the source file contains a single <Log> tag and inside a bunch of (chronologically sorted) messages. Anything more complicated would require some more work.
 
Thank you for your detailed answer. I followed your instructions and it did work once, but without showing dates and times. Whenever I use cmd+s in safari it says that it has to be saved using webarchive (and html), but when I open it, it's unreadable.
Is there any other option that might work?

edit:
I've found this link: https://gist.github.com/dwf/292030 with a script that should do exactly what I want. However, since I have almost zero knowledge of how to do anything on Mac, could someone please explain to me what to do with a script like this?

edit2:
https://magp.ie/2010/02/15/format-xml-with-textwrangler/ With this, it gets better organised in Textwrangler, but there are still too many other lines that are not important for just reading it. Is there a way to eliminate everything but the usernames, text, date and time?

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type='text/xsl' href='MessageLog.xsl'?>
<Log FirstSessionID="1" LastSessionID="29">
<Message Date="16.01.2005" Time="13:35:18" DateTime="2005-01-16T12:35:18.860Z" SessionID="1">
<From>
<User FriendlyName="User1"/>
</From>
<To>
<User FriendlyName="User2"/>
</To>
<Text Style="font-family:Arial; font-weight:bold; color:#800000; ">aaa1</Text>
</Message>
<Message Date="16.01.2005" Time="13:35:23" DateTime="2005-01-16T12:35:23.707Z" SessionID="1">
<From>
<User FriendlyName="User1"/>
</From>
<To>
<User FriendlyName="User2"/>
</To>
<Text Style="font-family:Arial; font-weight:bold; color:#800000; "aaa2</Text>
</Message>
 
Last edited:
Okay, let's use the script. I've edited it a bit to show the date. This is it now:
Code:
import sys
from xml.dom.minidom import parse

if len(sys.argv) != 2:
    print >>sys.stderr, "usage: " + sys.argv[0] + " <inputfile>"
    sys.exit(1)

doml = parse(sys.argv[1])
for message in doml.getElementsByTagName("Message"):
    fromNode = message.getElementsByTagName("From")[0]
    userNode = fromNode.getElementsByTagName("User")[0]
    name = "At " + message.getAttribute("Date") + ", " + message.getAttribute("Time") + ", " + userNode.getAttribute("FriendlyName") + \
        " says:"
    print name.encode('utf-8')
    msg = message.getElementsByTagName("Text")[0].firstChild.nodeValue
    print msg.encode('utf-8')
    print ""
Take the script, and Cmd+C copy it. Then open TextEdit and Cmd+V paste it. Don't change anything. Save the file as "script.py", without the quotes, to your Desktop. Then move your xml logs to the Desktop, too, to make things simple.
Now, the script is written in the Python programming language, that comes preinstalled with macOS, so it should run just fine.
Open Terminal. Your working directory is now your home dir. Your Desktop is right inside it so just write
Code:
cd Desktop
and hit return (enter).
Python scripts are run with this syntax: python file +arguments
The name of the file to be run is script.py and your only argument is the name of the log file. I don't know the name of your log files, so I'll just pretend one of them is called chat_log.xml
Type
Code:
python script.py chat_log.xml
then hit return (enter).

The script should print the conversation directly to your terminal. If you want to put it into a file, just change the command like this:
Code:
python script.py chat_log.xml > chat_log_out.txt
Snímek obrazovky 2018-03-07 v 16.49.56.png
 
  • Like
Reactions: rikscha
Okay, let's use the script. I've edited it a bit to show the date. This is it now:
Code:
import sys
from xml.dom.minidom import parse

if len(sys.argv) != 2:
    print >>sys.stderr, "usage: " + sys.argv[0] + " <inputfile>"
    sys.exit(1)

doml = parse(sys.argv[1])
for message in doml.getElementsByTagName("Message"):
    fromNode = message.getElementsByTagName("From")[0]
    userNode = fromNode.getElementsByTagName("User")[0]
    name = "At " + message.getAttribute("Date") + ", " + message.getAttribute("Time") + ", " + userNode.getAttribute("FriendlyName") + \
        " says:"
    print name.encode('utf-8')
    msg = message.getElementsByTagName("Text")[0].firstChild.nodeValue
    print msg.encode('utf-8')
    print ""
Take the script, and Cmd+C copy it. Then open TextEdit and Cmd+V paste it. Don't change anything. Save the file as "script.py", without the quotes, to your Desktop. Then move your xml logs to the Desktop, too, to make things simple.
Now, the script is written in the Python programming language, that comes preinstalled with macOS, so it should run just fine.
Open Terminal. Your working directory is now your home dir. Your Desktop is right inside it so just write
Code:
cd Desktop
and hit return (enter).
Python scripts are run with this syntax: python file +arguments
The name of the file to be run is script.py and your only argument is the name of the log file. I don't know the name of your log files, so I'll just pretend one of them is called chat_log.xml
Type
Code:
python script.py chat_log.xml
then hit return (enter).

The script should print the conversation directly to your terminal. If you want to put it into a file, just change the command like this:
Code:
python script.py chat_log.xml > chat_log_out.txt
View attachment 753595
Hello, I tried but Terminal shows that:


File "script.py", line 1

{\rtf1\ansi\ansicpg950\cocoartf1671\cocoasubrtf100

^

SyntaxError: unexpected character after line continuation character
 
Hello, I tried but Terminal shows that:


File "script.py", line 1

{\rtf1\ansi\ansicpg950\cocoartf1671\cocoasubrtf100

^

SyntaxError: unexpected character after line continuation character

You must have saved that file as RTF (rich text format), not as TXT (plaintext), which is not your fault, as that's what TextEdit does by default.

When you're about to save the script in TextEdit, make sure it's in the plaintext mode — in the bar at the top of the screen click Format, then Convert to plaintext (roughly, my OS is in Czech).

The TextEdit window is supposed to look like this:
Snímek obrazovky 2018-12-03 v 9.43.01.png
NOT like this:
Snímek obrazovky 2018-12-03 v 9.42.37.png

After you do that, save the file as script.py (DO NOT forget the .py extension) and proceed.
 
You must have saved that file as RTF (rich text format), not as TXT (plaintext), which is not your fault, as that's what TextEdit does by default.

When you're about to save the script in TextEdit, make sure it's in the plaintext mode — in the bar at the top of the screen click Format, then Convert to plaintext (roughly, my OS is in Czech).

The TextEdit window is supposed to look like this:
View attachment 808074
NOT like this:
View attachment 808075

After you do that, save the file as script.py (DO NOT forget the .py extension) and proceed.
Thank you very much!!
 
Okay, let's use the script. I've edited it a bit to show the date. This is it now:
Code:
import sys
from xml.dom.minidom import parse

if len(sys.argv) != 2:
    print >>sys.stderr, "usage: " + sys.argv[0] + " <inputfile>"
    sys.exit(1)

doml = parse(sys.argv[1])
for message in doml.getElementsByTagName("Message"):
    fromNode = message.getElementsByTagName("From")[0]
    userNode = fromNode.getElementsByTagName("User")[0]
    name = "At " + message.getAttribute("Date") + ", " + message.getAttribute("Time") + ", " + userNode.getAttribute("FriendlyName") + \
        " says:"
    print name.encode('utf-8')
    msg = message.getElementsByTagName("Text")[0].firstChild.nodeValue
    print msg.encode('utf-8')
    print ""
Take the script, and Cmd+C copy it. Then open TextEdit and Cmd+V paste it. Don't change anything. Save the file as "script.py", without the quotes, to your Desktop. Then move your xml logs to the Desktop, too, to make things simple.
Now, the script is written in the Python programming language, that comes preinstalled with macOS, so it should run just fine.
Open Terminal. Your working directory is now your home dir. Your Desktop is right inside it so just write
Code:
cd Desktop
and hit return (enter).
Python scripts are run with this syntax: python file +arguments
The name of the file to be run is script.py and your only argument is the name of the log file. I don't know the name of your log files, so I'll just pretend one of them is called chat_log.xml
Type
Code:
python script.py chat_log.xml
then hit return (enter).

The script should print the conversation directly to your terminal. If you want to put it into a file, just change the command like this:
Code:
python script.py chat_log.xml > chat_log_out.txt
View attachment 753595




Hi!

I did try to use the 'python script.py logfile.xml' in a different OS and then it returned:

File "script.py", line 14
print name.encode('utf-8')
^
SyntaxError: invalid syntax


Does this script works only in macOS?
BTW, Python 3.7.2 version.

Thanks!
 
Hi!

I did try to use the 'python script.py logfile.xml' in a different OS and then it returned:
...
Does this script works only in macOS?
BTW, Python 3.7.2 version.

Thanks!

Sounds like a Python version problem. Python 3 brought many breaking changes, including the different behavior of the print keyword. This script is supposed to be run with Python 2. (Both 2 and 3 are used in the wild, with 2 being phased out)
 
Okay, let's use the script. I've edited it a bit to show the date. This is it now:
Code:
import sys
from xml.dom.minidom import parse

if len(sys.argv) != 2:
    print >>sys.stderr, "usage: " + sys.argv[0] + " <inputfile>"
    sys.exit(1)

doml = parse(sys.argv[1])
for message in doml.getElementsByTagName("Message"):
    fromNode = message.getElementsByTagName("From")[0]
    userNode = fromNode.getElementsByTagName("User")[0]
    name = "At " + message.getAttribute("Date") + ", " + message.getAttribute("Time") + ", " + userNode.getAttribute("FriendlyName") + \
        " says:"
    print name.encode('utf-8')
    msg = message.getElementsByTagName("Text")[0].firstChild.nodeValue
    print msg.encode('utf-8')
    print ""
Take the script, and Cmd+C copy it. Then open TextEdit and Cmd+V paste it. Don't change anything. Save the file as "script.py", without the quotes, to your Desktop. Then move your xml logs to the Desktop, too, to make things simple.
Now, the script is written in the Python programming language, that comes preinstalled with macOS, so it should run just fine.
Open Terminal. Your working directory is now your home dir. Your Desktop is right inside it so just write
Code:
cd Desktop
and hit return (enter).
Python scripts are run with this syntax: python file +arguments
The name of the file to be run is script.py and your only argument is the name of the log file. I don't know the name of your log files, so I'll just pretend one of them is called chat_log.xml
Type
Code:
python script.py chat_log.xml
then hit return (enter).

The script should print the conversation directly to your terminal. If you want to put it into a file, just change the command like this:
Code:
python script.py chat_log.xml > chat_log_out.txt
View attachment 753595

Dekuji! This worked brilliantly. Any idea how I could colour format this easily in an automated fashion? I have. about 50 xml files so trying to see if I can run this through a script.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.