Parsing Mac XML PList into something readable

Viewed 5606

I am trying to pull data from XML PList (Apple System Profiler) files, and read it into a memory database, and finally I want to turn it into something human-readable.

The problem is that the format seems to be very difficult to read in a consistent manner. I have gone over a few solutions already, but I haven't found a solution yet that I found satisfying. I always end up having to hard code a lot of values, and end up having to many if-else/switch statements.

The format looks like this.

<plist>
    <key>_system</key>
    <array>
     <dict>
      <key>_cpu_type</key>
      <string>Intel Core Duo</string>
     </dict>
    </array>
</plist>

Example file here.

After I have read (or during reading), I make use of an internal dictionary that I use to determine what type of information it is. For example, if the key is cpu_type I save the information accordingly.


A few examples that I have tried (simplified) to pull the information.

 XmlTextReader reader = new
 XmlTextReader("C:\\test.spx");

 reader.XmlResolver = null;

 reader.ReadStartElement("plist");

 String key = String.Empty; String str
 = String.Empty;

 Int32 Index = 0;

 while (reader.Read()) {

     if (reader.LocalName == "key")
     {
         Index++;
         key = reader.ReadString();
     }
     else if (reader.LocalName == "string")
     {
         str = reader.ReadString();

         if (key != String.Empty)
         {
             dct.Add(Index, new KeyPair(key, str));
             key = String.Empty;
         }
     } 
}

Or something like this.

foreach (var d in xdoc.Root.Elements("plist"))
   dict.Add(d.Element("key").Value,> d.Element("string").Value);

I found a framework, that I may be able to modify here.


Some more useful information

Mac OS X Information on the system profiler here.

Apple script used to parse the XML-files here.


Any advise or insight into this would be deeply appreciated.

1 Answers
Related