Skip to content

Latest commit

 

History

History
33 lines (26 loc) · 924 Bytes

02-parsing_documents.md

File metadata and controls

33 lines (26 loc) · 924 Bytes

Parsing documents

DocumentFactory class is responsible for parsing documents. There are two methods:

  • parseDocument — takes an instance of the DOMDocument as argument and return one of the Document subclasses;
  • parseXML — takes string with XML as argument then uses parseDocument.
use Mekras\Atom\DocumentFactory;
use Mekras\Atom\Document\EntryDocument;
use Mekras\Atom\Document\FeedDocument;
use Mekras\Atom\Exception\AtomException;

$factory = new DocumentFactory;

$xml = file_get_contents('http://example.com/atom');
try {
    $document = $factory->parseXML($xml);
} catch (AtomException $e) {
    die($e->getMessage());
}

if ($document instanceof FeedDocument) {
    $feed = $document->getFeed();
    //...
} elseif ($document instanceof EntryDocument) {
    $entry = $document->getEntry();
    //...
}