Prewk\XmlStringStreamer\Parser\UniqueNode::getExtractedContainer PHP Method

getExtractedContainer() public method

Get the extracted container XML, if called before the whole stream is parsed, the XML returned can be invalid due to missing closing tags
public getExtractedContainer ( ) : string
return string XML string
    public function getExtractedContainer()
    {
        if (!$this->options["extractContainer"]) {
            throw new Exception("This method requires the 'extractContainer' option to be true");
        }
        return $this->containerXml;
    }

Usage Example

 public function test_UniqueNode_parser_with_pubmed_xml_and_container_extraction()
 {
     $file = __DIR__ . "/../../xml/pubmed-example.xml";
     $stream = new File($file, 512);
     $parser = new UniqueNode(array("uniqueNode" => "PubmedArticle", "extractContainer" => true));
     $streamer = new XmlStringStreamer($parser, $stream);
     $expectedPMIDs = array("24531174", "24529294", "24449586");
     $foundPMIDs = array();
     while ($node = $streamer->getNode()) {
         $xmlNode = simplexml_load_string($node);
         $foundPMIDs[] = (string) $xmlNode->MedlineCitation->PMID;
     }
     $this->assertEquals($expectedPMIDs, $foundPMIDs, "The PMID nodes should be as expected");
     $containerXml = simplexml_load_string($parser->getExtractedContainer());
     $this->assertEquals("PubmedArticleSet", $containerXml->getName(), "Root node should be as expected");
     $this->assertEquals("bar", $containerXml->attributes()->foo, "Attributes should be extracted correctly");
     $this->assertEquals("qux", $containerXml->attributes()->baz, "Attributes should be extracted correctly");
 }