473,394 Members | 2,084 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 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 3717
Dormilich
8,658 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,658 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,658 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

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

Similar topics

5
by: maxwell | last post by:
I used to program in matlab An read an image file with : fread(a,,0) which gave me a matrix of 256*256 full of numbers which were the pidxel values from 0 to 256. How do i do the same in c++ ...
2
by: George Ter-Saakov | last post by:
Hi. I need to read and analyze the header of the JPG file public struct _JFIFHeader { byte SOI1, SOI2; /* 00h Start of Image Marker */ byte APP01,APP02; /* 02h Application Use Marker */ short...
5
by: Alejandro Lapeyre | last post by:
I am writing a class to wrap a RIFF file, so i have to read and decode some structured data on the file. For example, in the begining of file is a header: Public Structure RiffHeader Public...
8
by: a | last post by:
I have a struct to write to a file struct _structA{ long x; int y; float z; } struct _structA A; //file open write(fd,A,sizeof(_structA)); //file close
1
by: deme | last post by:
the problem is: do an array of structure to store ur personal info! and the other one is do a structure of array to store ur personal info! guys if it is possible can u pls tell me how to do this? am...
2
by: HONOREDANCESTOR | last post by:
I tried to read a structure from a file, but I got this error message before I could compile it: "Error 103 Option Strict On disallows narrowing from type 'System.ValueType' to type...
9
by: vineeth | last post by:
Hello all, I have come across a weird problem, I need to determine the amount of bytes read from a file, but couldn't figure it out , My program does this : __ file = open("somefile") data =...
6
by: carles | last post by:
Hi, Here, sample code where a byte array is used to fill a particular structure: fs = File.OpenRead(path); // FileStream BITMAPFILEHEADER bfh = new BITMAPFILEHEADER(); b = new byte;
4
by: whygog | last post by:
Hi everyone, I'm having a pit of trouble with some c++ programming. I am trying to read in multiple strings from a file on a line, and I'm having trouble with new lines. What I am trying to do is...
1
by: eran yurman | last post by:
H, I need to call an API with the follwoing prototype: unsigned long CountDevices (BYTE *p_deviceSize,BYTE *p_device ); I have no knowledge of the content of the API function itself but...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.