Jyxo\XmlReader::getContent PHP Method

getContent() public method

Returns element's contents including tags.
public getContent ( ) : string
return string
    public function getContent() : string
    {
        if (self::ELEMENT === $this->nodeType && $this->isEmptyElement) {
            return '';
        }
        $depth = 1;
        $text = '';
        while (0 !== $depth && $this->read()) {
            if (isset($this->contentTypes[$this->nodeType])) {
                $text .= $this->value;
            } elseif (self::ELEMENT === $this->nodeType) {
                if ($this->isEmptyElement) {
                    $text .= '<' . $this->name . '/>';
                } else {
                    $depth++;
                    $text .= '<' . $this->name;
                    while ($this->moveToNextAttribute()) {
                        $text .= ' ' . $this->name . '="' . $this->value . '"';
                    }
                    $text .= '>';
                }
            } elseif (self::END_ELEMENT === $this->nodeType) {
                $depth--;
                if ($depth > 0) {
                    $text .= '</' . $this->name . '>';
                }
            }
        }
        return $text;
    }

Usage Example

Beispiel #1
0
 /**
  * Tests the getContent() method.
  *
  * @see \Jyxo\XmlReader::getContent()
  */
 public function testGetContent()
 {
     // In the form: tag (key), expected value (value)
     $tests = array();
     $tests['one'] = 'word';
     $tests['second'] = 'two <tag>words</tag>';
     $tests['third'] = 'three<empty/><tag>simple</tag> words';
     $tests['forth'] = "\n\t\tfour<tag>words</tag>on<empty/>several<empty/>lines\n\t";
     $tests['fifth'] = 'fifth test without tags';
     $tests['sixth'] = 'cdata in sixth test';
     $tests['seventh'] = 'cdata with <empty/><tag>tags</tag> in seventh test';
     $tests['eighth'] = 'eigth test with <tag attribute="value">tags and attributes</tag>';
     $tests['ninth'] = 'ninth test with <tag attribute="value">tags and attributes and <inner>inner tags</inner></tag>';
     $this->reader->open($this->path . '/content.xml');
     $this->reader->next('test');
     while ($this->reader->read()) {
         if ($this->reader->nodeType == \XMLReader::ELEMENT) {
             $this->assertEquals($tests[$this->reader->name], $this->reader->getContent());
         }
     }
 }