0
votes

I need to parse an XML file and I need also to parse the doctype. I've tried with XML Reader but when I found a nodetype 10 (doctype), I can't get it's value.

There is a way to extract the doctype from an XML file, with XMLReader?

Edit: as asked, some sample code. however is nothing rather than a dump, right now.

$reader = new XMLReader( );
$filename = 'test.xhtml';
$reader->open($filename);
while( $reader->read( ) )
{
    $nodeType = $reader->nodeType;
    $nodeName = $reader->name;
    $nodeValue = $reader->value;
    if( $nodeType == 10 )
    {
        echo $nodeType ."\n";
        echo $nodeName ."\n";
        echo $nodeValue ."\n";
        echo $reader->localName ."\n";
        echo $reader->namespaceURI ."\n";
        echo $reader->prefix ."\n";
        echo $reader->xmlLang ."\n";
        echo $reader->readString() . "\n";
        echo $reader->readInnerXML() . "\n";
        while( $reader->moveToNextAttribute( ) )
        {
            echo $reader->name . "=" . $reader->value;
        }
}
2
Try a var_dump() of the reader object? - Marc B
@Steve81 Can you post some code please - brian_d

2 Answers

0
votes

You can use DOM to read the DOCTYPE data:

$doc = new DOMDocument();
$doc->loadXML($xmlData);
var_dump($doc->doctype->publicId);
var_dump($doc->doctype->systemId);
var_dump($doc->doctype->name);
var_dump($doc->doctype->entities);
var_dump($doc->doctype->notations);
0
votes

I have not found a way to do this with XMLReader despite a lot of looking. However you can use DOMDocument to read the doctype quite easily, then revert to XMLReader to read the rest of the stream. For example, to get the system ID part of the doctype before processing the rest of the XML file:

$doc = new DOMDocument();
$doc->load($xmlfile);
$systemId = $doc->doctype->systemId;
unset($doc);

// Then proceed with XMLReader:
$reader = new XMLReader();
$reader->open($xmlfile);
while($reader->read())
{
    // etc

I suppose that this may not be practical in all circumstances but it worked for me while processing very large XML files for which I needed to read the system ID from the doctype.