471,616 Members | 1,095 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,616 software developers and data experts.

How to read unknown XML structure file to Array

abdoelmasry
104 100+
Hi Pros

I need help to read xml to array,

xml file structure is unknown, and has many elements and subelements,

so it should be imported to multiple array.

here is xml file as example:
file name: test.xml
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="windows-1256" ?>
  2. <books id="1">
  3. <category id="1" name="cat 1">
  4. <book id="1" name="cat 1 book1">
  5. <minibook id="1" name="cat 1 - book1 - minibook 1"></minibook>
  6. <minibook id="2" name="cat 1 - book1 - minibook 2"></minibook>
  7. <minibook id="3" name="cat 1 - book1 - minibook 3"></minibook>
  8. <minibook id="4" name="cat 1 - book1 - minibook 4"></minibook>
  9. </book>
  10. <book id="2" name="cat 1 book2">
  11. <minibook id="1" name="cat 1 - book2 - minibook 1"></minibook>
  12. <minibook id="2" name="cat 1 - book2 - minibook 2"></minibook>
  13. <minibook id="3" name="cat 1 - book2 - minibook 3"></minibook>
  14. <minibook id="4" name="cat 1 - book2 - minibook 4"></minibook>
  15. </book>
  16. <book id="3" name="cat 1 book3">
  17. <minibook id="1" name="cat 1 - book3 - minibook 1"></minibook>
  18. <minibook id="2" name="cat 1 - book3 - minibook 2"></minibook>
  19. <minibook id="3" name="cat 1 - book3 - minibook 3"></minibook>
  20. <minibook id="4" name="cat 1 - book3 - minibook 4"></minibook>
  21. </book>
  22. </category>
  23. <category id="2" name="cat 2">
  24. <book id="1" name="cat 2 book1">
  25. <minibook id="1" name="cat 2 - book1 - minibook 1"></minibook>
  26. <minibook id="2" name="cat 2 - book1 - minibook 2"></minibook>
  27. <minibook id="3" name="cat 2 - book1 - minibook 3"></minibook>
  28. <minibook id="4" name="cat 2 - book1 - minibook 4"></minibook>
  29. </book>
  30. <book id="2" name="cat 2 book2">
  31. <minibook id="1" name="cat 2 - book2 - minibook 1"></minibook>
  32. <minibook id="2" name="cat 2 - book2 - minibook 2"></minibook>
  33. <minibook id="3" name="cat 2 - book2 - minibook 3"></minibook>
  34. <minibook id="4" name="cat 2 - book2 - minibook 4"></minibook>
  35. </book>
  36. <book id="3" name="cat 2 book3">
  37. <minibook id="1" name="cat 2 - book3 - minibook 1"></minibook>
  38. <minibook id="2" name="cat 2 - book3 - minibook 2"></minibook>
  39. <minibook id="3" name="cat 2 - book3 - minibook 3"></minibook>
  40. <minibook id="4" name="cat 2 - book3 - minibook 4"></minibook>
  41. </book>
  42. </category>
  43. <category id="3" name="cat 3">
  44. <book id="1" name="cat 3 book1">
  45. <minibook id="1" name="cat 3 - book1 - minibook 1"></minibook>
  46. <minibook id="2" name="cat 3 - book1 - minibook 2"></minibook>
  47. <minibook id="3" name="cat 3 - book1 - minibook 3"></minibook>
  48. <minibook id="4" name="cat 3 - book1 - minibook 4"></minibook>
  49. </book>
  50. <book id="2" name="cat 3 book2">
  51. <minibook id="1" name="cat 3 - book2 - minibook 1"></minibook>
  52. <minibook id="2" name="cat 3 - book2 - minibook 2"></minibook>
  53. <minibook id="3" name="cat 3 - book2 - minibook 3"></minibook>
  54. <minibook id="4" name="cat 3 - book2 - minibook 4"></minibook>
  55. </book>
  56. <book id="3" name="cat 3 book3">
  57. <minibook id="1" name="cat 3 - book3 - minibook 1"></minibook>
  58. <minibook id="2" name="cat 3 - book3 - minibook 2"></minibook>
  59. <minibook id="3" name="cat 3 - book3 - minibook 3"></minibook>
  60. <minibook id="4" name="cat 3 - book3 - minibook 4"></minibook>
  61. </book>
  62. </category>
  63. <category id="4" name="cat 4">
  64. <book id="1" name="cat 4 book1">
  65. <minibook id="1" name="cat 4 - book1 - minibook 1"></minibook>
  66. <minibook id="2" name="cat 4 - book1 - minibook 2"></minibook>
  67. <minibook id="3" name="cat 4 - book1 - minibook 3"></minibook>
  68. <minibook id="4" name="cat 4 - book1 - minibook 4"></minibook>
  69. </book>
  70. <book id="2" name="cat 4 book2">
  71. <minibook id="1" name="cat 4 - book2 - minibook 1"></minibook>
  72. <minibook id="2" name="cat 4 - book2 - minibook 2"></minibook>
  73. <minibook id="3" name="cat 4 - book2 - minibook 3"></minibook>
  74. <minibook id="4" name="cat 4 - book2 - minibook 4"></minibook>
  75. </book>
  76. <book id="3" name="cat 4 book3">
  77. <minibook id="1" name="cat 4 - book3 - minibook 1"></minibook>
  78. <minibook id="2" name="cat 4 - book3 - minibook 2"></minibook>
  79. <minibook id="3" name="cat 4 - book3 - minibook 3"></minibook>
  80. <minibook id="4" name="cat 4 - book3 - minibook 4"></minibook>
  81. </book>
  82. </category>
  83. <category id="5" name="cat 5">
  84. <book id="1" name="cat 5 book1">
  85. <minibook id="1" name="cat 5 - book1 - minibook 1"></minibook>
  86. <minibook id="2" name="cat 5 - book1 - minibook 2"></minibook>
  87. <minibook id="3" name="cat 5 - book1 - minibook 3"></minibook>
  88. <minibook id="4" name="cat 5 - book1 - minibook 4"></minibook>
  89. </book>
  90. <book id="2" name="cat 5 book2">
  91. <minibook id="1" name="cat 5 - book2 - minibook 1"></minibook>
  92. <minibook id="2" name="cat 5 - book2 - minibook 2"></minibook>
  93. <minibook id="3" name="cat 5 - book2 - minibook 3"></minibook>
  94. <minibook id="4" name="cat 5 - book2 - minibook 4"></minibook>
  95. </book>
  96. <book id="3" name="cat 5 book3">
  97. <minibook id="1" name="cat 5 - book3 - minibook 1"></minibook>
  98. <minibook id="2" name="cat 5 - book3 - minibook 2"></minibook>
  99. <minibook id="3" name="cat 5 - book3 - minibook 3"></minibook>
  100. <minibook id="4" name="cat 5 - book3 - minibook 4"></minibook>
  101. </book>
  102. </category>
  103. </books>
  104.  
my php code is not working, here is it:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. //get Element attributes
  4. function get_attributes($element){
  5. $attrib_array=array();
  6. $element_attributes=$element->attributes;
  7. foreach($element_attributes as $attrib){
  8. $attrib_array[$attrib->name]=$attrib->value;
  9. }
  10. return $attrib_array;
  11. }
  12.  
  13. //get xml Elements to array
  14. function get_elements($Elements_list){
  15. foreach($Elements_list as $xml_element){
  16. // NodeType (1) means this is Element not #text or CDATA
  17. if($xml_element->nodeType=='1'){
  18. //get Element id for Array Key
  19. $elemid=$xml_element->getAttribute('id');
  20. $elemtagname=$xml_element->tagName;
  21. $currentlevel=array();
  22. //Get Element Attributes if it has
  23. //If this element Has Attributes it will be added to Array '$attr_array'
  24. $attr_array=array();
  25. if($xml_element->hasAttributes()){
  26. $attr_array=$this->get_attributes($xml_element);
  27. }
  28. $currentlevel[$elemid]['tagname']=$xml_element->tagName;
  29. $currentlevel[$elemid]['attribute']=$attr_array;
  30. $currentlevel[$elemid]['content']=$xml_element->nodeValue;
  31. //get child Nodes for every Element (if exists)
  32. if($xml_element->hasChildNodes()){
  33. $currentlevel[$elemid]['child']=$this->get_elements($xml_element->childNodes);
  34. }
  35. }
  36. }
  37. return $currentlevel;
  38. }
  39.  
  40. //cals Function
  41. $xml_file=new DOMDocument();
  42. $xml_file->load("test.xml");
  43. $xml_childs=$xml_file->getElementsByTagName("books");
  44. $xml_to_array=get_elements($xml_childs);
  45.  
  46. // Array $xml_to_array should hold all Xml data
  47. ?>
  48.  
the problem is : i cann't build xml data (elements,child nodes) properly to array.

thank you
Feb 19 '11 #1
7 3572
Dormilich
8,656 Expert Mod 8TB
what do you need the array for?
Feb 19 '11 #2
abdoelmasry
104 100+
i am php developer, i am working a lot with xml files,

for example: i develop php script works with 100 xml files,

by the normal way, i should be know the file structure to use this way:

getElementByTagname();

and that will force me to write 100 functions to read the 100 xml files,

i need to create class to read any xml file, it will be so easy to pass xml files path to class, and class read all data and dump it to array
Feb 19 '11 #3
You can convert XML to Json and Json to array.
Feb 19 '11 #4
abdoelmasry
104 100+
it should be by PHP5, no client side codes.
Feb 19 '11 #5
Dormilich
8,656 Expert Mod 8TB
SimpleXML offers a fine XML reading class.
Feb 19 '11 #6
abdoelmasry
104 100+
i checked it before coming here,

no reference to Read xml file to Multi Dimensional Array.
Feb 21 '11 #7
Dormilich
8,656 Expert Mod 8TB
normally you donít need to convert XML to an multidimensional array. you have Iterators for that which can operate on the SimpleXMLElement objects.
Feb 21 '11 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by maxwell | last post: by
2 posts views Thread by George Ter-Saakov | last post: by
5 posts views Thread by Alejandro Lapeyre | last post: by
8 posts views Thread by a | last post: by
2 posts views Thread by HONOREDANCESTOR | last post: by
9 posts views Thread by vineeth | last post: by
6 posts views Thread by carles | last post: by
1 post views Thread by ZEDKYRIE | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.