Box\Spout\Reader\Wrapper\SimpleXMLElement::getFirstChildByTagName PHP Method

getFirstChildByTagName() public method

Returns the first child matching the given tag name
public getFirstChildByTagName ( string $tagName ) : SimpleXMLElement | null
$tagName string
return SimpleXMLElement | null The first child matching the tag name or NULL if none found
    public function getFirstChildByTagName($tagName)
    {
        $doesElementExist = isset($this->simpleXMLElement->{$tagName});
        /** @var \SimpleXMLElement $realElement */
        $realElement = $this->simpleXMLElement->{$tagName};
        return $doesElementExist ? $this->wrapSimpleXMLElement($realElement) : null;
    }

Usage Example

Example #1
0
    /**
     * @return void
     */
    public function testRemoveNodeMatchingXPath()
    {
        $xml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<worksheet>
    <sheetData>
        <row r="1">
            <c r="A1"><v>0</v></c>
            <c r="A2"><v>1</v></c>
        </row>
    </sheetData>
</worksheet>
XML;
        $element = new SimpleXMLElement($xml);
        $this->assertNotNull($element->getFirstChildByTagName('sheetData'));
        $element->removeNodesMatchingXPath('//sheetData');
        $this->assertNull($element->getFirstChildByTagName('sheetData'));
    }