How to resolve “XML Parsing Error at 1:1. Error 4: Empty document” when installing a Joomla template or extension
Posted by Jim DeLaHunt on 08 Feb 2009 at 11:21 pm | Tagged as: Joomla, robobait
Recently, I remodelled a Joomla template for one of my clients. As I was installing newer versions of the template, I noticed an error message started appearing on installation:
XML Parsing Error at 1:1. Error 4: Empty document
However, the installation seemed to be successful. And I had a valid templateDetails.xml file in my template’s .zip file. There was a Joomla! forum thread “XML Parsing Error at 1:1. Error 4: Empty document” dating from December 28, 2008 about this problem, so it wasn’t just me.
Here is my diagnosis of the problem, and my solution. Since this is one of those problems that drove me crazy, I’m going to put my findings here in hopes that search engines will find it and bring it to others who might benefit from the tip. Hence, it’s tagged “robobait”.
The short answer: the error message comes from a metadata file named ._templateDetails.xml, which the Mac OX 10.5.x Finder inserted into the ZIP archive. This file didn’t have XML content, but the Joomla installer interpreted it as such. And the content it did have, happened to be the kind of invalid content that provoked an error message instead of silent failure. The immediate solution is to generate the ZIP file in such a way that there is no metadata file named ._templateDetails.xml . Long-term, it would be nice if Joomla! would not display an error message in this situation.
Read on for more about the problem, the diagnosis, and possible solutions.
The installable archive structure
Joomla templates (and other extensions like modules and plugins) are structured as a compressed archive (.zip, .tar.gz, or .tar.bz2 formats all work) containing the various files of the extension, plus an XML-format file which gives meta-information about the extension. This meta-information includes the name of the extension, the version number, a list of the files used by the extension, and so on.
As I made revisions of the template, I generated the compressed archive installable using Mac OS X 10.5.x Finder. I selected all the files and directories of the template, right-clicked on one of the file icons, and selected “Compress”. Finder produced a file “Archive.zip”, which I renamed.
I had forgotten that Mac OS X archive utilities like compress and tar use the AppleDouble file format by default. AppleDouble stores metadata and resource forks for files in sidecar files with a “._” prefix followed by the main file’s name. They put these in a subdirectory tree __MACOSX. Thus my archives had an 82-byte file __MACOSX/._templateDetails.xml in addition to the correct templateDetails.xml. See OSX Considered harmful for someone else who found this behaviour an obstacle. The Mac OS X archive utilities look at a shell environment variable “COPYFILE_DISABLE” (was “COPY_EXTENDED_ATTRIBUTES_DISABLE” in Mac OS 10.3) to disable the generation of the AppleDouble sidecar files. See the blog Resource forks and tar for more on the subject.
The Joomla installer code
The error, “XML Parsing Error at 1:1. Error 4: Empty document”, appears to be thrown by routine JSimpleXML::_handleerror(), which is called by JSimpleXML::_parse(), which is a thin wrapper around PHP’s xml_parse(), which in turn is a port of the expat XML parsing library. See more at the JSimpleXML class documentation.
In particular, JSimpleXML::_handleerror() line 256 contains the statement:
JError::raiseWarning( 'SOME_ERROR_CODE' , 'XML Parsing Error at '.$line.':'.$col.'. Error '.$code.': '.xml_error_string($code));
where $line, $col, and $code are all variables passed back from PHP’s xml_parse error routines. Code 4 appears to be expat’s XML_ERROR_INVALID_TOKEN per PHP’s XML parsing documentation, and the expat.h header file. However, PHP’s xml_error_string() gives the string “Empty document” for this code. Reasonable, perhaps; it looks like expat returns XML_ERROR_INVALID_TOKEN for empty files and invalid UTF-8 code sequences, and an empty file might be the more common occurrence of the two.
Thus, it looks like it is a file which is empty or has an invalid UTF-8 code sequence that is provoking the error message. But my templateDetails.xml was valid UTF-8 (valid US-ASCII, in fact). So what file was provoking the error?
I believe (but haven’t confirmed by stepping through with a debugger) that when you ask Joomla to install a template, Joomla creates a JInstaller object, and calls JInstaller::install(“/path/to/install/file.zip”) or similar. This seems evenually to call a method JInstaller::_findManifest(). (There is also a JInstallerTemplate class, with a method JInstallerTemplate::install(). This method also seems eventually to call the same _findManifest().)
JInstaller::_findManifest() finds all files ending in the extension “.xml”. Thus it would find both the correct templateDetails.xml, and the bogus ._templateDetails.xml. From line 1103 in that method:
// Get an array of all the xml files from teh installation directory $xmlfiles = JFolder::files($this->getPath('source'), '.xml
The method then calls JSimpleXML::_parse() on each one of the files. It looks like there was something about the ._templateDetails.xml file which caused the parser to throw an error, not just return a failure code. This provokes JSimpleXML to produce the message, XML Parsing Error at 1:1. Error 4: Empty document. The method notes the error message, and crosses that file from its list. Then the method parses the correct templateDetails.xml file to complete the installation.
I observed this error message in my client’s site, which runs Joomla! 1.5.9 site. Just for fun I tried installing that very same template in an experimental Joomla 1.5.3 site which is close to a bare install of the basic Joomla distribution. It installed with no error message. I then upgraded that experimental site to Joomla 1.5.9 using the 1.5.3-> 1.5.9 upgrader. I uninstalled the template, then installed it again. This time, the error message occured. This makes me think that Joomla! 1.5.9 behaves differently than 1.5.3 in some way which displays an error for my ._templateDetails.xml file. I’m guessing that 1.5.9 is more diligent about passing error messages on to the user.
Solutions
What could Joomla! do to prevent this problem? I think it would be helpful if Joomla would extend this error message to include a file name. That alone would have saved me hours of debugging: thinking my real templateDetails.xml file was at fault, I stripped it down, checked its validity, and of course found nothing. Joomla could also perhaps not pass on such error messages as long as one of the XML files in the archive is valid. It could even change its pattern to exclude the AppleDouble metadata files. Since the second parameter to JFolder::files is eventually passed to PHP’s preg_match(), maybe “^[^\.][^_].*xml$” would do the trick. (This regexp also excludes file names like “a.xml”, but those aren’t legal names for a Joomla! manifest anyway.)
To fix this problem as a template developer, try one of:
- Use some platform other than Mac OS X to generate your installable archives.
- On Mac OS X, compress from the shell. Execute export COPYFILE_DISABLE=true first, then your compress command like zip -r ../mytemplate.zip *.
May no-one else have to spend the hours I spent diagnosing this problem!
, 1, true);The method then calls JSimpleXML::_parse() on each one of the files. It looks like there was something about the ._templateDetails.xml file which caused the parser to throw an error, not just return a failure code. This provokes JSimpleXML to produce the message, XML Parsing Error at 1:1. Error 4: Empty document. The method notes the error message, and crosses that file from its list. Then the method parses the correct templateDetails.xml file to complete the installation.
I observed this error message in my client’s site, which runs Joomla! 1.5.9 site. Just for fun I tried installing that very same template in an experimental Joomla 1.5.3 site which is close to a bare install of the basic Joomla distribution. It installed with no error message. I then upgraded that experimental site to Joomla 1.5.9 using the 1.5.3-> 1.5.9 upgrader. I uninstalled the template, then installed it again. This time, the error message occured. This makes me think that Joomla! 1.5.9 behaves differently than 1.5.3 in some way which displays an error for my ._templateDetails.xml file. I’m guessing that 1.5.9 is more diligent about passing error messages on to the user.
Solutions
What could Joomla! do to prevent this problem? I think it would be helpful if Joomla would extend this error message to include a file name. That alone would have saved me hours of debugging: thinking my real templateDetails.xml file was at fault, I stripped it down, checked its validity, and of course found nothing. Joomla could also perhaps not pass on such error messages as long as one of the XML files in the archive is valid. It could even change its pattern to exclude the AppleDouble metadata files. Since the second parameter to JFolder::files is eventually passed to PHP’s preg_match(), maybe “^[^\.][^_].*xml$” would do the trick. (This regexp also excludes file names like “a.xml”, but those aren’t legal names for a Joomla! manifest anyway.)
To fix this problem as a template developer, try one of:
- Use some platform other than Mac OS X to generate your installable archives.
- On Mac OS X, compress from the shell. Execute export COPYFILE_DISABLE=true first, then your compress command like zip -r ../mytemplate.zip *.
May no-one else have to spend the hours I spent diagnosing this problem!
I’ve just filed a Joomla! tracker item #14982 (http://joomlacode.org/gf/project/joomla/tracker/?action=TrackerItemEdit&tracker_item_id=14982) about this issue.
Great! Thanks for all the codes and configurations you have provided here in your article.
Hi,
I was also getting these XML parsing errors on my OSX machine and I have a simple solution for it.
You can use muCommander (www.mucommander.com), a filemanager with the good old Norton Commander interface, to open these zip files and delete the “hidden” osx files. It’s also possible to create zip files with it, if you do so it will not include the hidden osx files!
Have fun.
Thanks for the info, i knew it was related to the way our MAC archived the files, but your explanation is awesome.
I have added a link on our forum to your site explaining this info.
Thanks again
Derek Buntin
Perfect Designing Ltd
This is exactly what I was looking for. Joomla is a top notch CMS and this is a perfect example why.
It was with relief I found this post as I think this what happened. I can no longer make changes in the Global config. I get the 500. Also in the components uninstall window I get XML Parsing Error at 1:1. Error 4: Empty document. I run a Mac and I think I may have rezipped something recently and uploaded it but can’t be sure. Anyway I didn’t read a fix, just preventions and recommendation for Joomla. Can anyone advise as to how to undo the damage?
John, I’m sorry to hear that you are having error 500’s. I think you might be experiencing a different problem than the Mac OS X metadata files.
In my experience, the metadata files cause an error message but the install proceeds anyhow. What gets installed doesn’t have the metadata files.
I suggest you take this problem to the Joomla forum “General Questions/New to Joomla”, if you haven’t done that already.
Thanks Jim,
A thorough, technical explanation – most of which was over my head, but I get the idea!
Installation of another compression utility, as suggested by Leander, would get rid of the error, but I’m content to wait it out until the fix is included a future Joomla release. At least I know it’s not something I’m doing wrong.
Appreciate the many hours you’ve spent chasing this down!
I used Leander’s solution. Works perfect.
Thank you both so much!!!
Before Joomla team corrects this, you can easily find invalid XML files using:
find . -name \*.xml -exec xmllint -valid -noout {} \;
first of all, thank u very much for this important tip.
I have the same problem but i’m not a template designer, i just downloaded it and want to use the template. Could I just delete the __MAXOSX directory and compress it again in winXP?
thanks
Hi, “thanks”. Glad you found this note useful. Yes, on Windows XP I believe you should be able to:
1. unzip the template
2. delete every file beginning with “._”, especially those ending in “.xml”.
3. zip the remaining files up into an archive.
As I read the AppleDouble specification, I don’t see a mention of a “__MAXOSX” directory. But if you have one, yes, try deleting it also.
Hello.
i make a template in pc machine. I get a same message in (XML Parsing Error at 1:1. Error 4: Not well-formed (invalid token))
The files in Mac OS machine to include in zip file with _ (underscore). xml its not found to pc zip file version.
Please help….
Thanks
Gekoum, I notice that your error message is different than the one I write about. Your message ends with “Not well-formed (invalid token))”, while mine ends with “Empty document”.
Perhaps you have a spelling error in one of your XML tokens.
If you’d like more help, a good place to go is the Joomla forums. For instance, there is a Greek language forum at http://forum.joomla.org/viewforum.php?f=50&sid=d39074dc2454076017415e2680f35d99 .
Hallo everybody,
you can also use keka to compress files in OSX. Download it at: http://www.kekaosx.com/de/
When you start it you can check “delete Mac resource files” and the upload into Joomla works…
RE: Leander
muCommander worked great for me too. Thanks!!!!
—————–
#
on 15 Apr 2009 at 00:06 Leander
Hi,
I was also getting these XML parsing errors on my OSX machine and I have a simple solution for it.
You can use muCommander (www.mucommander.com), a filemanager with the good old Norton Commander interface, to open these zip files and delete the “hidden†osx files. It’s also possible to create zip files with it, if you do so it will not include the hidden osx files!
Have fun.
Dang, got the same issue, but I don’t know if I can solve it at all. Maybe by removing all the ._-files by hand, but that’d be a pain.