Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [funkcja] array to xml
menic
post 29.08.2007, 17:09:32
Post #1





Grupa: Zarejestrowani
Postów: 493
Pomógł: 0
Dołączył: 14.06.2003
Skąd: Tomaszów Lubelski/Rzeszów

Ostrzeżenie: (0%)
-----


Mała funkcja która tworzy nam xml na podstawie tablicy php. Postarałem się, aby tworzyła poprawnie również elementy, które nie są unikalne. Trzeba tylko przekazać jako ostatni parametr tablicę z nazwami elementów non-unique. Nie obsługuje atrybutów (może kiedyś winksmiley.jpg ).
  1. <?php
  2. /**
  3.  * Tworzy obiekt xml na podstawie tablicy
  4.  *
  5.  * @param array $aXml Wejściowa tablica
  6.  * @param SimpleXMLElement $oXml
  7.  * @param array $aElements tablica non-unique elementów
  8.  * @return SimpleXMLElement
  9.  */
  10. function arrayToXml( array $aXml, SimpleXMLElement $oXml, $aElements = null )
  11. {
  12. static $aArray;
  13.  
  14. if( is_array( $aElements ) )
  15. {
  16. $aArray = $aElements; 
  17. }
  18.  
  19. if( is_array( $aXml ) )
  20. {
  21. foreach( $aXml as $k => $v )
  22. { 
  23. if( is_array( $v ) )
  24. {
  25. if( is_array( $aArray ) )
  26. {
  27. if( in_array( $k, $aArray ) )
  28. {
  29. foreach( $v as $kk => $vv )
  30. {
  31. $oXml->addChild( $k, $vv );
  32. }
  33. }
  34. else 
  35. {
  36. $oXml->addChild( $k );
  37. }
  38. }
  39. else 
  40. {
  41. $oXml->addChild( $k );
  42. }
  43. arrayToXml( $aXml[$k], $oXml->$k );
  44. }
  45. else 
  46. {
  47. if( !is_numeric( $k ) )
  48. {
  49. $oXml->addChild( $k, $v );
  50. }
  51. else 
  52. {
  53. //Tutaj można coś zrobić dla kluczy numerycznych
  54. }
  55. }
  56. }
  57. }
  58. return $oXml;
  59. }
  60. ?>


--------------------
Jak masz cos zrobic dobrze...
...To musisz zrobić to sam.

Uchwycić moment...
Go to the top of the page
+Quote Post
rafalp
post 22.04.2011, 13:58:05
Post #2





Grupa: Zarejestrowani
Postów: 224
Pomógł: 18
Dołączył: 4.02.2003
Skąd: Częstochowa

Ostrzeżenie: (0%)
-----


Czy można jakoś łatwo to w drugą stronę zrobić?


--------------------
Go to the top of the page
+Quote Post
shinuexx
post 27.07.2011, 19:23:22
Post #3





Grupa: Zarejestrowani
Postów: 78
Pomógł: 9
Dołączył: 2.02.2011
Skąd: undefined

Ostrzeżenie: (0%)
-----


jeśli cię to pocieszy znalazłem coś takiego:

  1. <?php
  2. ################################################################################
    ###
  3. #
  4. # XML Library, by Keith Devens, version 1.2b
  5. # <a href="http://keithdevens.com/software/phpxml" target="_blank">http://keithdevens.com/software/phpxml</a>
  6. #
  7. # This code is Open Source, released under terms similar to the Artistic License.
  8. # Read the license at <a href="http://keithdevens.com/software/license" target="_blank">http://keithdevens.com/software/license</a>
  9. #
  10. ################################################################################
    ###
  11.  
  12. ################################################################################
    ###
  13. # XML_unserialize: takes raw XML as a parameter (a string)
  14. # and returns an equivalent PHP data structure
  15. ################################################################################
    ###
  16. function & XML_unserialize(&$xml){
  17. $xml_parser = &new XML();
  18. $data = &$xml_parser->parse($xml);
  19. $xml_parser->destruct();
  20. return $data;
  21. }
  22. ################################################################################
    ###
  23. # XML_serialize: serializes any PHP data structure into XML
  24. # Takes one parameter: the data to serialize. Must be an array.
  25. ################################################################################
    ###
  26. function & XML_serialize(&$data, $level = 0, $prior_key = NULL){
  27. if($level == 0){ ob_start(); echo '<?xml version="1.0" ?>',"\n"; }
  28. while(list($key, $value) = each($data))
  29. if(!strpos($key, ' attr')) #if it's not an attribute
  30. #we don't treat attributes by themselves, so for an empty element
  31. # that has attributes you still need to set the element to NULL
  32.  
  33. if(is_array($value) and array_key_exists(0, $value)){
  34. XML_serialize($value, $level, $key);
  35. }else{
  36. $tag = $prior_key ? $prior_key : $key;
  37. echo str_repeat("\t", $level),'<',$tag;
  38. if(array_key_exists("$key attr", $data)){ #if there's an attribute for this element
  39. while(list($attr_name, $attr_value) = each($data["$key attr"]))
  40. echo ' ',$attr_name,'="',htmlspecialchars($attr_value),'"';
  41. reset($data["$key attr"]);
  42. }
  43.  
  44. if(is_null($value)) echo " />\n";
  45. elseif(!is_array($value)) echo '>',htmlspecialchars($value),"</$tag>\n";
  46. else echo ">\n",XML_serialize($value, $level+1),str_repeat("\t", $level),"</$tag>\n";
  47. }
  48. reset($data);
  49. if($level == 0){ $str = &ob_get_contents(); ob_end_clean(); return $str; }
  50. }
  51. ################################################################################
    ###
  52. # XML class: utility class to be used with PHP's XML handling functions
  53. ################################################################################
    ###
  54. class XML{
  55. var $parser; #a reference to the XML parser
  56. var $document; #the entire XML structure built up so far
  57. var $parent; #a pointer to the current parent - the parent will be an array
  58. var $stack; #a stack of the most recent parent at each nesting level
  59. var $last_opened_tag; #keeps track of the last tag opened.
  60.  
  61. function XML(){
  62. $this->parser = &xml_parser_create();
  63. xml_parser_set_option(&$this->parser, XML_OPTION_CASE_FOLDING, false);
  64. xml_set_object(&$this->parser, &$this);
  65. xml_set_element_handler(&$this->parser, 'open','close');
  66. xml_set_character_data_handler(&$this->parser, 'data');
  67. }
  68. function destruct(){ xml_parser_free(&$this->parser); }
  69. function & parse(&$data){
  70. $this->document = array();
  71. $this->stack = array();
  72. $this->parent = &$this->document;
  73. return xml_parse(&$this->parser, &$data, true) ? $this->document : NULL;
  74. }
  75. function open(&$parser, $tag, $attributes){
  76. $this->data = ''; #stores temporary cdata
  77. $this->last_opened_tag = $tag;
  78. if(is_array($this->parent) and array_key_exists($tag,$this->parent)){ #if you've seen this tag before
  79. if(is_array($this->parent[$tag]) and array_key_exists(0,$this->parent[$tag])){ #if the keys are numeric
  80. #this is the third or later instance of $tag we've come across
  81. $key = count_numeric_items($this->parent[$tag]);
  82. }else{
  83. #this is the second instance of $tag that we've seen. shift around
  84. if(array_key_exists("$tag attr",$this->parent)){
  85. $arr = array('0 attr'=>&$this->parent["$tag attr"], &$this->parent[$tag]);
  86. unset($this->parent["$tag attr"]);
  87. }else{
  88. $arr = array(&$this->parent[$tag]);
  89. }
  90. $this->parent[$tag] = &$arr;
  91. $key = 1;
  92. }
  93. $this->parent = &$this->parent[$tag];
  94. }else{
  95. $key = $tag;
  96. }
  97. if($attributes) $this->parent["$key attr"] = $attributes;
  98. $this->parent = &$this->parent[$key];
  99. $this->stack[] = &$this->parent;
  100. }
  101. function data(&$parser, $data){
  102. if($this->last_opened_tag != NULL) #you don't need to store whitespace in between tags
  103. $this->data .= $data;
  104. }
  105. function close(&$parser, $tag){
  106. if($this->last_opened_tag == $tag){
  107. $this->parent = $this->data;
  108. $this->last_opened_tag = NULL;
  109. }
  110. array_pop($this->stack);
  111. if($this->stack) $this->parent = &$this->stack[count($this->stack)-1];
  112. }
  113. }
  114. function count_numeric_items(&$array){
  115. return is_array($array) ? count(array_filter(array_keys($array), 'is_numeric')) : 0;
  116. }
  117. ?>
Go to the top of the page
+Quote Post
panisher
post 27.04.2012, 13:17:55
Post #4





Grupa: Zarejestrowani
Postów: 2
Pomógł: 0
Dołączył: 3.12.2009
Skąd: Lubin

Ostrzeżenie: (0%)
-----


  1. function array2xml($array, $xml = false){
  2.  
  3. if($xml === false){
  4.  
  5. $xml = new SimpleXMLElement('<?xml version=\'1.0\' encoding=\'utf-8\'?><'.key($array).'/>');
  6. $array = $array[key($array)];
  7.  
  8. }
  9. foreach($array as $key => $value){
  10. if(is_array($value)){
  11. $this->array2xml($value, $xml->addChild($key));
  12. }else{
  13. $xml->addChild($key, $value);
  14. }
  15. }
  16. return $xml->asXML();
  17. }


--------------------
Tworzenie stron internetowych: freelancer - panisher.pl
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 17.04.2024 - 00:49