Convert XML into an array object

Source: Internet
Author: User
Convert XML into an array object
After the XML content is parsed, a corresponding array object is returned, and the return type [array, JSON] can be set through parameter settings. default value: Array
Because simplexml_load_string-based parsing of XML with namespaces is not very good, the namespace content will be lost. the above is the conclusion obtained in the test. this problem will be solved in later versions.
This can meet general needs.

  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 conflicts with the built-in attribute, you can customize the relevant attribute name.
  20. // $ Xml_to_array-> setAttributeAsName ("attributeAsName")-> setContentAsName ("contentasName ");
  21. $ R = $ xml_to_array-> parseXml (true );
  22. Print_r ($ r );
  23. }
  24. // Print the result:
  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 the 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 conflicts with the built-in attribute, you can customize the relevant attribute name.
  46. // $ Xml_to_array-> setAttributeAsName ("attributeAsName")-> setContentAsName ("contentasName ");
  47. $ R = $ xml_to_array-> parseXml ();
  48. Print_r ($ r );
  49. }
  50. // Print the result
  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. )

  1. /**
  2. * Created by JetBrains PhpStorm.
  3. * User: hedgehog
  4. * Date: 12-5-9
  5. * Time: PM
  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 attributes on the 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 the hierarchical relationship
  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. // Append
  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. }

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.