/framework/core/xml/XmlParser.php

http://zoop.googlecode.com/ · PHP · 63 lines · 49 code · 12 blank · 2 comment · 1 complexity · d238dc9c6976129aeed447ea8bbca0ce MD5 · raw file

  1. <?php
  2. abstract class XmlParser
  3. {
  4. private $dom;
  5. protected $container;
  6. function __construct()
  7. {
  8. $this->dom = new XmlDom();
  9. }
  10. function parseText($xml)
  11. {
  12. $xml = "<xml>$xml</xml>";
  13. $root = $this->dom->parseText($xml);
  14. return $this->handleRoot($root);
  15. }
  16. function parseFile($filename)
  17. {
  18. $root = $this->dom->parseFile($filename);
  19. return $this->handleRoot($root);
  20. }
  21. protected abstract function initExtra();
  22. protected abstract function handleTextNode($child, $container, $extra);
  23. private function handleRoot($root)
  24. {
  25. // echo 'handleRoot 1<br>';
  26. $this->container = $this->initRootContainer();
  27. // echo 'handleRoot 2<br>';
  28. $extra = $this->initExtra();
  29. $this->handleNode($root, $this->container, $extra);
  30. return $this->container;
  31. }
  32. protected function handleXml($node, $container, $extra)
  33. {
  34. return $container;
  35. }
  36. private function handleNode($node, $container, $extra)
  37. {
  38. $this->preNodeHandler($node, $container, $extra);
  39. $tagName = $node->getName();
  40. $handler = "handle$tagName";
  41. $container = $this->$handler($node, $container, $extra);
  42. foreach($node->children as $child)
  43. {
  44. if($child->isText())
  45. $this->handleTextNode($child, $container, $extra);
  46. else
  47. $this->handleNode($child, $container, $extra);
  48. }
  49. $this->postNodeHandler($node, $container, $extra);
  50. }
  51. }