tags:

views:

101

answers:

2

I'm trying to fully validate an xml file which may be published by a user before it's fully published and live - it's basically somewhat like a sitemap.xml and it absolutely can't be published without being error-proof ( yes, I do have my own custom dtd for it as well ).

I was also thinking of implementing a storage system so it would store say, the last 2-3 versions just incase ( as it's a real high priority type of thing ).

Any tips?

Edit: Here's what I currently have but in some circumstances I believe it validated when the xml wasn't exactly valid:

$dom = new DOMDocument();
if ( $dom->load( $tempFileName ) ) {
    if ( $dom->validate() ) {
    echo '<p>XML is valid. Overwriting sitemap.xml.</p>';
    file_put_contents( 'sitemap.xml', $sitemapXML->asXML() );
    } else {
    echo '<p>XML is not valid. Please correct.</p>';
    }
}
+2  A: 

Using the DOM methods:

$doc = new DOMDocument();
$doc->load($xmlPath);
if (!$doc->validate()) {
    die("OH NOES!");
    // ... or perform your own restore-to-a-backup procedure.
}
nickf
I'm using this exact method, but I'm pretty sure I ran into some issue ( maybe with ampersands or encoding? ) where it published without being completely valid/well-formed. Or could it be that I was mistaken?
meder
+1  A: 

You could "tell" libxml to load the dtd and validate against it immediately when the xml document is loaded.

$doc->load( $tempFileName, LIBXML_DTDLOAD|LIBXML_DTDVALID )

see http://php.net/libxml.constants

VolkerK