getSections($config), true) . ' ?>'; } public function getSections($filename) { $result = array(); $book = new SimpleXMLElement(file_get_contents($filename)); //preface $id = (string) $book->preface['id']; $title = (string) $book->preface->title; $result[$id] = array( 'title'=>$title, 'parent' => null, 'path' => $title ); //preface's sections foreach ($book->preface->section as $section) { $result += $this->parseSection($section, $id, $title); } //chapters foreach ($book->chapter as $chapter) { $result += $this->parseChapter($chapter); } return $result; } protected function parseChapter(SimpleXMLElement $chapter) { $result = array(); $id = (string) $chapter['id']; $title = ++$this->chapterNumber .'. '. (string) $chapter->title; $result[$id] = array( 'title'=> $title, 'parent' => null, 'path' => $title, ); foreach ($chapter->section as $section) { $result += $this->parseSection($section, $id, $title); } return $result; } protected function parseSection(SimpleXMLElement $section, $parentId, $path) { $result = array(); $id = (string) $section['id']; $title = (string) $section->title; $path .= ' / ' . (string) $section->title; $result[$id] = array( 'title' => $title, 'parent' => $parentId, 'path' => $path, ); if ($section->section) { foreach ($section->section as $innerSection) { $result += $this->parseSection($innerSection, $id, $path); } } return $result; } } ?>