You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
2.8 KiB

21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
21 years ago
  1. <?php
  2. /** @file xml_xpath_tree.php
  3. * @brief XML XPath Tree example
  4. * @ingroup Examples
  5. * @author Marcus Boerger
  6. * @date 2003 - 2006
  7. * @version 1.0
  8. *
  9. * Usage: php xml_tree.php \<file\> \<xpath\> [\<len\>]
  10. *
  11. * Simply specify the xml file to tree with parameter \<file\>.
  12. *
  13. * The second parameter is a xpath expression to be evaluated.
  14. *
  15. * The third parameter allows to output a maximum of \<len\> characters of
  16. * the element or attribute value.
  17. */
  18. if ($argc < 3) {
  19. echo <<<EOF
  20. Usage: php ${_SERVER['PHP_SELF']} <file>
  21. Displays a graphical tree for the elements in <file> <xpath> [<len>].
  22. <file> The file for which to generate the tree graph.
  23. <xpath> An XPath expression to evaluate.
  24. <len> Max length to display for attribute and element content (default: 0).
  25. EOF;
  26. exit(1);
  27. }
  28. if (!class_exists("RecursiveTreeIterator", false)) require_once("recursivetreeiterator.inc");
  29. /** @ingroup Examples
  30. * @brief Generates the XML structure for SimpleXMLXPathResult elements
  31. * @version 1.0
  32. */
  33. class SimpleXpathStructure extends AppendIterator implements RecursiveIterator
  34. {
  35. function __construct($xml)
  36. {
  37. parent::__construct();
  38. if (!is_null($xml))
  39. {
  40. $xml = is_object($xml) ? $xml : $xml = simplexml_load_file($xml, 'SimpleXmlIterator');
  41. $this->append($xml);
  42. $attr = $xml->attributes();
  43. if ($attr)
  44. {
  45. $this->append($attr);
  46. }
  47. }
  48. }
  49. function key()
  50. {
  51. return ($this->getIteratorIndex() ? '@' : '') . parent::key();
  52. }
  53. function hasChildren()
  54. {
  55. return $this->getInnerIterator()->hasChildren()
  56. || !$this->getIteratorIndex();
  57. }
  58. function getChildren()
  59. {
  60. $inner = $this->getInnerIterator();
  61. return $this->getIteratorIndex()
  62. ? $inner->getChildren()
  63. : new SimpleXmlStructure($inner->getChildren());
  64. }
  65. function current()
  66. {
  67. global $len;
  68. if ($len)
  69. {
  70. $val = substr(parent::current(), 0, $len);
  71. $nws = preg_replace("/([\r\n]|\s\s+)/e", " ", $val);
  72. return ($this->getIteratorIndex() ? '= ' : ': ') . $nws;
  73. }
  74. else
  75. {
  76. return '';
  77. }
  78. }
  79. }
  80. /** @ingroup Examples
  81. * @brief Generates a structure from an xpath query on a XML file
  82. * @version 1.0
  83. */
  84. class SimpleXMLXPathResult extends RecursiveArrayIterator
  85. {
  86. /**
  87. * @param $file XML file to open
  88. * @param $xpath query to execute
  89. */
  90. function __construct($file, $xpath)
  91. {
  92. $xml = @simplexml_load_file($file, 'SimpleXmlIterator');
  93. parent::__construct($xml ? $xml->xpath($xpath) : NULL);
  94. }
  95. function current()
  96. {
  97. return '';
  98. }
  99. function hasChildren()
  100. {
  101. return true;
  102. }
  103. function getChildren()
  104. {
  105. return new SimpleXpathStructure(parent::current());
  106. }
  107. }
  108. $len = isset($argv[3]) ? $argv[3] : 0;
  109. $xml = new SimpleXMLXPathResult($argv[1], $argv[2]);
  110. $it = new RecursiveTreeIterator($xml, RecursiveTreeIterator::BYPASS_CURRENT);
  111. foreach($it as $c=>$v)
  112. {
  113. echo "$c$v\n";
  114. }
  115. ?>