XML to Array Object

Source: Internet
Author: User
Tags tag name
The XML content is parsed and returned with a corresponding array object, and the return type "array, JSON" default can be set by the parameter setting: array
because it is not good to parse XML with namespaces based on Simplexml_load_string, it will lose namespace content the above is the conclusion that is obtained in the test, and subsequent versions will solve this problem.
This can meet the general needs of the use of
  1. function ToJSON ()
  2. {
  3. Require_once '. /classes/xmltoarray.php ';
  4. $XML =<<<>
  5. "-//apache software foundation//dtd Struts Configuration 2.0//en"
  6. "Http://struts.apache.org/dtds/struts-2.0.dtd" >
  7. Add.action
  8. /emp/add_suc.jsp
  9. /emp/list.jsp
  10. Delete.action
  11. /emp/delete_suc.jsp
  12. Update.action
  13. /emp/edit_suc.jsp
  14. /emp/edit.jsp
  15. XML;
  16. Header ("content-type:text/html; Charset=utf-8 ");
  17. $xml _to_array = new Xmltoarray ();
  18. $xml _to_array->setxml ($XML);
  19. When the tag name is in conflict with the built-in property, you can customize the name of the relevant property, and the general profile does not need to be set
  20. $xml _to_array->setattributeasname ("Attributeasname")->setcontentasname ("Contentasname");
  21. $r = $xml _to_array->parsexml (true);
  22. Print_r ($R);
  23. }
  24. Printing results:
  25. {"Struts": {"Attributes": [], "Content": "", "constant": {"attributes": {"name": "Struts.objectfactory", "Value": " Spring "}," Content ":" "}," package ": {" attributes ": {" name ":" Crm_employee "," Extends ":" Struts-default "," namespace ":" \/emp "}," Content ":" "," action ": [{" Attributes ": {" name ":" Add "," Class ":" Addbean "," Method ":" Add "}," Content ":" "," Result ": [{" Attributes ": []," content ":" Add.action "},{" Attributes ": []," content ":" \/emp\/add_suc.jsp "}]},{" Attributes ": {" name ":" List "," Class ":" Listbean "," Method ":" List "}," Content ":" "," result ": {" Attributes ": []," content ":" \/emp\/list.jsp "}},{" attributes ": {" name ":" Delete "," Class ":" Deletebean "," Method ":" Delete "}," Content ":" "," Result ": {" Attributes ": []," content ":" \/emp\/delete_suc.jsp "}},{" attributes ": {" name ":" Update "," Class ":" Updatebean "," Method ":" Update "}," Content ":" "," result ": {" Attributes ": []," content ":" \/emp\/edit_suc.jsp "}},{" Attributes ": {" name ":" Edit "," Class ":" Editbean "," Method ":" Edit "}," Content ":" "," result ": {" Attributes ": []," content ":" \/emp\/edit.jsp "}}]}}
  26. Print array
  27. function ToArray ()
  28. {
  29. Require_once '. /classes/xmltoarray.php ';
  30. $XML =<<<>
  31. "-//apache software foundation//dtd Struts Configuration 2.0//en"
  32. "Http://struts.apache.org/dtds/struts-2.0.dtd" >
  33. Add.action
  34. /emp/add_suc.jsp
  35. /emp/list.jsp
  36. Delete.action
  37. /emp/delete_suc.jsp
  38. Update.action
  39. /emp/edit_suc.jsp
  40. /emp/edit.jsp
  41. XML;
  42. Header ("content-type:text/html; Charset=utf-8 ");
  43. $xml _to_array = new Xmltoarray ();
  44. $xml _to_array->setxml ($XML);
  45. When the tag name is in conflict with the built-in property, you can customize the name of the relevant property, and the general profile does not need to be set
  46. $xml _to_array->setattributeasname ("Attributeasname")->setcontentasname ("Contentasname");
  47. $r = $xml _to_array->parsexml ();
  48. Print_r ($R);
  49. }
  50. Print results
  51. Array
  52. (
  53. [Struts] = Array
  54. (
  55. [Attributes] = = Array
  56. (
  57. )
  58. [Content] =
  59. [constant] = = Array
  60. (
  61. [Attributes] = = Array
  62. (
  63. [Name] = Struts.objectfactory
  64. [Value] = Spring
  65. )
  66. [Content] =
  67. )
  68. [Package] = Array
  69. (
  70. [Attributes] = = Array
  71. (
  72. [Name] = Crm_employee
  73. [Extends] = Struts-default
  74. [Namespace] =/emp
  75. )
  76. [Content] =
  77. [Action] = = Array
  78. (
  79. [0] = = Array
  80. (
  81. [Attributes] = = Array
  82. (
  83. [Name] = + Add
  84. [Class] = Addbean
  85. [Method] = + add
  86. )
  87. [Content] =
  88. [result] = = Array
  89. (
  90. [0] = = Array
  91. (
  92. [Attributes] = = Array
  93. (
  94. )
  95. [Content] = Add.action
  96. )
  97. [1] = = Array
  98. (
  99. [Attributes] = = Array
  100. (
  101. )
  102. [Content] =/emp/add_suc.jsp
  103. )
  104. )
  105. )
  106. [1] = = Array
  107. (
  108. [Attributes] = = Array
  109. (
  110. [Name] = List
  111. [Class] = Listbean
  112. [Method] = List
  113. )
  114. [Content] =
  115. [result] = = Array
  116. (
  117. [Attributes] = = Array
  118. (
  119. )
  120. [Content] =/emp/list.jsp
  121. )
  122. )
  123. [2] = = Array
  124. (
  125. [Attributes] = = Array
  126. (
  127. [name] + = delete
  128. [Class] = Deletebean
  129. [Method] = delete
  130. )
  131. [Content] =
  132. [result] = = Array
  133. (
  134. [Attributes] = = Array
  135. (
  136. )
  137. [Content] =/emp/delete_suc.jsp
  138. )
  139. )
  140. [3] = = Array
  141. (
  142. [Attributes] = = Array
  143. (
  144. [Name] = Update
  145. [Class] = Updatebean
  146. [Method] = Update
  147. )
  148. [Content] =
  149. [result] = = Array
  150. (
  151. [Attributes] = = Array
  152. (
  153. )
  154. [Content] =/emp/edit_suc.jsp
  155. )
  156. )
  157. [4] = = Array
  158. (
  159. [Attributes] = = Array
  160. (
  161. [Name] = + Edit
  162. [Class] = Editbean
  163. [Method] = edit
  164. )
  165. [Content] =
  166. [result] = = Array
  167. (
  168. [Attributes] = = Array
  169. (
  170. )
  171. [Content] =/emp/edit.jsp
  172. )
  173. )
  174. )
  175. )
  176. )
  177. )
Copy Code
  1. /**
  2. * Created by JetBrains Phpstorm.
  3. * User:hedgehog
  4. * date:12-5-9
  5. * Time: 4:37
  6. * To change this template use File | Settings | File Templates.
  7. */
  8. Class Xmltoarray
  9. {
  10. Private $xml;
  11. Private $contentAsName = "content";
  12. Private $attributesAsName = "attributes";
  13. Private $xml _array = Array ();
  14. Public Function Setxml ($XMLSTR)
  15. {
  16. $this->xml = $xmlstr;
  17. return $this;
  18. }
  19. Public Function Setcontentasname ($name)
  20. {
  21. $this->contentasname = $name;
  22. return $this;
  23. }
  24. Public Function Setattributeasname ($name)
  25. {
  26. $this->attributesasname = $name;
  27. return $this;
  28. }
  29. Private Function Createxmlarray ($node,& $parent _node, $node _index =0)
  30. {
  31. $node _attrbutes= Array ();
  32. $node _name = $node->getname ();
  33. $attributes = $node->attributes ();
  34. $children = $node->children ();
  35. Traverse all properties on a node
  36. foreach ($attributes as $attrname = $attrvalue)
  37. {
  38. $attrvalue = (string) $attrvalue;
  39. $node _attrbutes[$attrname] = Trim ($attrvalue);
  40. }
  41. $content = "";
  42. if (count ($children) = = 0)
  43. {
  44. $content = (string) $node;
  45. }
  46. $node _array = Array (
  47. $this->attributesasname = $node _attrbutes,
  48. $this->contentasname = Trim ($content)
  49. );
  50. Set up hierarchical relationships
  51. if (!isset ($parent _node[$node _name]))
  52. {
  53. $is = count ($parent _node);
  54. if (!isset ($parent _node[$this->attributesasname]) && count ($parent _node) > 0)
  55. {
  56. $last _index = count ($parent _node)-1;
  57. $parent _node =& $parent _node[$last _index];
  58. $parent _node[$node _name] = $node _array;
  59. }
  60. Else
  61. {
  62. $parent _node[$node _name] = $node _array;
  63. }
  64. }
  65. Else
  66. {
  67. $append = & $parent _node[$node _name];
  68. if (Isset ($append [$this->attributesasname])
  69. {
  70. $parent _node[$node _name] = array ($append);
  71. $append = & $parent _node[$node _name];
  72. }
  73. if (Isset ($append [$node _index])
  74. {
  75. $append = & $append [$node _index];
  76. }
  77. Additional
  78. Array_push ($append, $node _array);
  79. }
  80. $index = 0;
  81. Recursive operation
  82. foreach ($children as $childnode)
  83. {
  84. $parent = & $parent _node[$node _name];
  85. $this->createxmlarray ($childnode, $parent, $index + +);
  86. }
  87. return $parent _node;
  88. }
  89. Public Function Parsexml ($isjson =false)
  90. {
  91. $root = simplexml_load_string ($this->xml);
  92. $parent _node = Array ();
  93. $array = $this->createxmlarray ($root, $parent _node);
  94. Return $isjson? Json_encode ($array): $array;
  95. }
  96. }
Copy Code
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.