Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
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.

73 lines
2.0 KiB

  1. <?php
  2. /**
  3. * @brief This class contain functions to work with XML data
  4. *
  5. */
  6. class xml {
  7. function from_array($array, &$xml, $remove_header = false, $namespaces = array(), $root = true) {
  8. if ($root) {
  9. foreach($array as $key => $value) {
  10. foreach ($namespaces AS $nskey => $nsvalue)
  11. $key .= " xmlns".($nskey == "" ? "":":").$nskey.'="'.$nsvalue.'"';
  12. $root = new SimpleXMLElement("<".$key."/>");
  13. self::from_array($value, $root, $remove_header, $namespaces, false);
  14. $dom = dom_import_simplexml($root)->ownerDocument;
  15. $dom->formatOutput = true;
  16. $xml = $dom;
  17. $xml_text = $dom->saveXML();
  18. if ($remove_header)
  19. $xml_text = trim(substr($xml_text, 21));
  20. return $xml_text;
  21. }
  22. }
  23. foreach($array as $key => $value) {
  24. if ($key == "@attributes") {
  25. if (!isset($element) OR !is_array($value))
  26. continue;
  27. foreach ($value as $attr_key => $attr_value) {
  28. $element_parts = explode(":", $attr_key);
  29. if ((count($element_parts) > 1) AND isset($namespaces[$element_parts[0]]))
  30. $namespace = $namespaces[$element_parts[0]];
  31. else
  32. $namespace = NULL;
  33. $element->addAttribute ($attr_key, $attr_value, $namespace);
  34. }
  35. continue;
  36. }
  37. $element_parts = explode(":", $key);
  38. if ((count($element_parts) > 1) AND isset($namespaces[$element_parts[0]]))
  39. $namespace = $namespaces[$element_parts[0]];
  40. else
  41. $namespace = NULL;
  42. if (!is_array($value))
  43. $element = $xml->addChild($key, xmlify($value), $namespace);
  44. elseif (is_array($value)) {
  45. $element = $xml->addChild($key, NULL, $namespace);
  46. self::from_array($value, $element, $remove_header, $namespaces, false);
  47. }
  48. }
  49. }
  50. function copy(&$source, &$target, $elementname) {
  51. if (count($source->children()) == 0)
  52. $target->addChild($elementname, xmlify($source));
  53. else {
  54. $child = $target->addChild($elementname);
  55. foreach ($source->children() AS $childfield => $childentry)
  56. self::copy($childentry, $child, $childfield);
  57. }
  58. }
  59. }
  60. ?>