вопрос следующий как получить доступ к текстовым NODE , Text1 , Text3, Text5, Text4, Text4-2?!
Есть решение, но оно отдает ноды как внутриннеие элементы тегов в данном случае html, body, div, span! Т.е. например в коде "<div>Text4 <span>Text4-1</span> Text4-2</div>", нода для внешнего дива получается "Text4 Text4-1 Text4-2", а необходимо получать доступ по отделности к каждому элементу Text.
Пример кода:
public function parse(){
//create DOM object
$doc = new DOMDocument();
@$doc->loadHTMLFile($this->link);
//create array with tags and content
$this->walkDom($doc);
}
private function walkDom($node, $level = 0){// recursive function for content array
if($node->nodeType != XML_TEXT_NODE && $node->nodeName != '#document' && $node->nodeName != 'br'){
//creating array
$this->content[$this->count] = array('depth' => $level, 'tag' => $node->nodeName, 'content' => (trim($node->textContent)!='')?$node->textContent:'No content');
$attributes = $node->attributes;
if(!empty($attributes)){
foreach($attributes as $attribute){
$this->content[$this->count]['attr'] = array($attribute->name => $attribute->value);
}
}else{
$this->content[$this->count]['attr'] = 'No attributes';
}
}
//... and go deeper
$cNodes = $node->childNodes;
if (count($cNodes) > 0){
$level++ ; // go one level deeper
$this->count++;
foreach($cNodes as $cNode){
$this->walkDom($cNode, $level); //recursion
}
$level = $level - 1; // come a level up
}
}