Validating XML files, including Google sitemap and atom XML using PHP

Posted on August 11th, 2010 in PHP | No Comments »

The following method is pulled from a class, in a php command line tool, that I created to generate google sitemap files and also Google Base atom files.

What I wanted to do was, vailidate the generated xml files against a schema, before saving them.

You can do this via the command using xmllint.

xmllint –schema atom.xsd.xml –valid –noout /xmlfiles/.test.xml.tmp

However you can easily do this in PHP.

Here are the schema files.

siteindex
sitemap
atom.xsd


static function validate_google_xml($xmlfile, $type)
{

print("Validating {$xmlfile} \n");

$xdoc = new DomDocument;
if($type == 'siteindex')
{
$schema = self::$scriptDir.'siteindex.xsd';
@$xdoc->Load($xmlfile);
if (@$xdoc->schemaValidate($schema))
{
return true;
}
}elseif($type == 'sitemap')
{
$schema = self::$scriptDir.'sitemap.xsd';
//echo $schema ; die;
@$xdoc->Load($xmlfile);
if (@$xdoc->schemaValidate($schema))
{
return true;
}

}elseif($type == 'atom')
{
$schema = self::$scriptDir.'atom.xsd.xml';
//echo $schema ; die;
@$xdoc->Load($xmlfile);
if (@$xdoc->schemaValidate($schema))
{
return true;
}

}

}

Share/Save/Bookmark

Generating a file list, for a Joomla Component XML info file

Posted on December 8th, 2009 in Apple OSX, Linux, PHP | No Comments »

I’ve had to do some work for a company recently.

It involved me creating Joomla components and those annoying XML files, listing all the files and folders to install.

If you want a quick command to generate the xml, you can cd to the directory then run

find * -type f -exec  echo ‘<filename>’{}’</filename>’ \;

find * -type d -exec  echo ‘<folder>’{}’</folder>’ \;

Then you can copy the output from the terminal and paste between the files tags.

<files folder=”components/com_test”>
<filename>controller.php</filename>
<filename>css/test.css</filename>
<filename>views/test/tmpl/index.html</filename>
<filename>test.php</filename>
<folder>css</folder>
<folder>images</folder>
<folder>install_sql</folder>
<folder>models</folder>
<folder>player</folder>
<folder>views</folder>
<folder>views/user</folder>
<folder>views/user/tmpl</folder>
<folder>views/test</folder>
<folder>views/test/tmpl</folder>
</files>

I repeated this for the administrator/components/com_test and components/com_test folders

Share/Save/Bookmark