Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: array 2 xml
Forum PHP.pl > Forum > Przedszkole
dado
znalazłem klasę typu array2xml ale nie ma do niej żadnego opisu albo choćby jednego przykładu zastosowania jak ją użyć. Zacząlem się interesować obiektami ale jestem jeszce troche zielony w tym temacie .
Proszę o pomoc w zastosowaniu klasy.
a oto ona

  1. <?php
  2. class array2xml
  3. {
  4. var $config = array(
  5. 'encoding' => 'ISO-8859-15',
  6. 'xmlns' => array(
  7. 'ino' => 'http://namespaces.softwareag.com/tamino/response2'
  8. )
  9. );
  10. function array2xml( $array )
  11. {
  12. if (!is_array($array)) return false;
  13. $this->array = $array;
  14. $this->dom = domxml_new_doc(&#092;"1.0\");
  15. }
  16.  
  17. function setEncoding( $enc )
  18. {
  19. $this->config['encoding'] = ( $enc != '' ) ? $enc : $this->config['encoding'];
  20. }
  21.  
  22. function addNamespaces( $assoc )
  23. {
  24. $this->config['xmlns'] = array_merge($this->config['xmlns'], $assoc);
  25. }
  26.  
  27. function getResult($format = TRUE)
  28. {
  29. $doc_root = array_shift( array_keys($this->array) );
  30. $root_element = $this->dom->create_element($doc_root);
  31. $this->_recArray2Node($root_element, $this->array[$doc_root]);
  32. $this->dom->append_child($root_element);
  33.  
  34. // check for namespaces ? add each to doc
  35. if ( is_array($this->used_namespaces) )
  36. foreach ($this->used_namespaces as $ns)
  37. $root_element->add_namespace($this->config[&#092;"xmlns\"][ $ns ], $ns);
  38.  
  39. // <b>Warning</b>: dump_mem(): xmlDocDumpFormatMemoryEnc: Failed to identify encoding handler for character set 'ISO-8859-15'
  40. return $this->dom->dump_mem($format,$this->config['encoding']);
  41. }
  42.  
  43. function _recArray2Node( $parent_node, $array )
  44. {
  45. foreach ($array as $key => $value)
  46. {
  47. $org_key = $key;
  48. list( $ns, $key ) = split( ':', str_replace(&#092;"@\",\"\",$org_key) );
  49. if ( !$key )
  50. $key = $ns;
  51. elseif ($ns == &#092;"xmlns\")
  52. {
  53. $this->addNamespaces( array($key => $value) );
  54. break;
  55. }else{
  56. if ( $this->config[&#092;"xmlns\"][ $ns ] )
  57. {
  58. $this->used_namespaces[] = $ns;
  59. $key = $ns.&#092;":\".$key;
  60. }
  61. else
  62. die(&#092;"Namespace for $ns does not exist! Use obj->addNamespaces( $assoc ) for adding.\");
  63. }
  64.  
  65. if (substr($org_key, 0, 1) == '@')
  66. {
  67. // attribute
  68. $parent_node->set_attribute( $key, $value );
  69. continue;
  70. }
  71. else if ( $key == '#text' || !is_array($value) )
  72. {
  73. // text node
  74. // check if valid text & not empty
  75. if ( $value=='0' | !empty($value) )
  76. {
  77. $element = $this->dom->create_text_node($value);
  78. $parent_node->append_child($element);
  79. }
  80. continue;
  81. } else {
  82. // child node
  83. // check for enumeration
  84. $enum = FALSE;
  85. while (list( $k, $v ) = each( $value ))
  86. {
  87. if ( is_numeric($k) )
  88. {
  89. // enumeration of multiple nodes
  90. $enum = TRUE;
  91. $element = $this->dom->create_element($key);
  92. $this->_recArray2Node($element, $v);
  93. $parent_node->append_child($element);
  94. }
  95. }
  96.  
  97. // check for enumeration
  98. if ( $enum == FALSE )
  99. {
  100. $element = $this->dom->create_element($key);
  101. $this->_recArray2Node($element, $value);
  102. $parent_node->append_child($element);
  103. }
  104. }
  105. }
  106. }
  107. }
  108.  
  109. ?>


skrobnąłem mały skrpycik test.php ale nie działa bo wynik jest obiektem a nie xml-em
  1. <?php
  2. include(&#092;"class.array2xml.php\");
  3.  
  4. $tbl[0]=&#092;"asds\";
  5. $tbl[1]=&#092;"fsdfsd\";
  6. $tbl[2]=&#092;"sfds\";
  7.  
  8. $ar = new array2xml($tbl);
  9.  
  10. echo $ar;
  11. ?>
Tajny
Sproboj tak:

  1. <?
  2. include(&#092;"class.array2xml.php\");
  3. $tbl[0]=&#092;"asds\";
  4. $tbl[1]=&#092;"fsdfsd\";
  5. $tbl[2]=&#092;"sfds\";
  6. $ar = new array2xml($tbl);
  7.  
  8. //i teraz xml 
  9.  
  10. $dane_xml=$ar->getResult();
  11. ?>
dado
zrobiłem tak jak radziłeś.
w przglądarce są takie baty

Cytat
Warning: Missing argument 1 for array2xml() in /home/tatrolub/public_html/w/class.array2xml.php on line 10

Warning: Invalid argument supplied for foreach() in /home/tatrolub/public_html/w/class.array2xml.php on line 45
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.