my best guess would be that the PSD is constructed similar to a archive, while using a xml file to keep track of its content. As for the layers they could just as well be png files & not only, depending on file contents. However this is just a supposition. I might just as well be wrong. I'm kind of working on something similar atm. I need to read the contents of the psd file, create a xml which would be a "summary" of the file, with layer description like "position","size" and "layer name" in order to use that xml for an automated layer extraction/export function.
EDIT: Might want to have a look at GIMP. That's one opensource image editor which has PSD support. You might want to try and get more answers there.
EDIT2: might wanna take a look at this:
http://www.fileformat.info/format/psd/egff.htm[
^]
there is a description of how the psd stores data. Thing is it might be outdated. I would use a combo of raw data from the psd and the info there to parse the file.